我用Phalcon框架创建了一个新项目。
首先,我的环境是:
Phalcon 3.2.12
Windows 10
Xampp / PHP 7.2
目前,我已在索引中创建了一个登录页面。当我启动我的应用程序时,我收到了这条消息:
致命错误:未捕获的Phalcon \ Mvc \ Dispatcher \ Exception:Dispatcher具有 检测到导致稳定性问题的循环路由 \公共\的index.php:32
第32行:
echo $application->handle()->getContent();
services.php
中有我的调度员:
$di->setShared('dispatcher', function () use ($di) {
$evManager = $di->getShared('eventsManager');
$evManager->attach("dispatch:beforeException", function ($event, $dispatcher, $exception) {
switch ($exception->getCode()) {
case PhDispatcher::EXCEPTION_HANDLER_NOT_FOUND:
case PhDispatcher::EXCEPTION_ACTION_NOT_FOUND:
$dispatcher->forward(array(
'controller' => 'index',
'action' => 'show404'
));
return false;
}
});
$dispatcher = new PhDispatcher();
$dispatcher->setEventsManager($evManager);
return $dispatcher;
});
我的IndexController
:
class IndexController extends ControllerBase
{
public function indexAction()
{
$this->view->setLayout('login');
}
public function show404Action()
{
$this->view->setLayout('login');
$this->response->setStatusCode(404, "Not Found");
$this->view->pick('index/404');
}
}
我的ControllerBase
:
public function beforeExecuteRoute($dispatcher)
{
$isValid = true;
// check auth
if ($dispatcher->getControllerName() !== 'index') {
$isValid = false;
$this->view->disable();
$this->response->setStatusCode(401, 'Vous devez vous identifier pour accéder à votre environnement.');
if (!$this->request->isAjax()) {
$this->response->redirect('index');
}
}
return $isValid;
}
的index.php
define('APP_PATH', realpath('..'));
/**
* Define APPLICATION_ENV (DEV,STAGING,PREPROD,PROD)
*
* @var APPLICATION_ENV string
*/
defined('APPLICATION_ENV') || define('APPLICATION_ENV', (isset($_SERVER['APPLICATION_ENV']) ? $_SERVER['APPLICATION_ENV'] : 'PROD'));
/**
* Read the configuration
*/
$config = include APP_PATH . "/app/config/" . strtolower(APPLICATION_ENV) . ".config.php";
/**
* Read auto-loader
*/
include APP_PATH . "/app/config/loader.php";
/**
* Read services
*/
include APP_PATH . "/app/config/services.php";
/**
* Handle the request
*/
$application = new \Phalcon\Mvc\Application($di);
echo $application->handle()->getContent();
如果我在方法var_dump
的开头加beforeExecuteRoute
,我看不到var_dump
的结果。
有你的想法吗?
由于
答案 0 :(得分:0)
可能你会找到一些无法找到的东西。尝试将 show404 重命名为 showNotFound ,您可以在afterExecuteRoute事件期间检查$ dispatcher-> getControllerName()和getActionName(),以查看为什么您的处理程序或操作永远不会找到了
public function showNotFoundAction()
{
$this->view->setLayout('login');
$this->response->setStatusCode(404, "Not Found");
$this->view->pick('index/404');
}
并且
$dispatcher->forward(array(
'controller' => 'index',
'action' => 'showNotFound'
));