use*_*244 2 php phalcon phalcon-routing
当我输入url时:
http://localhost/asdfasdfasdcxccarf
Phalcon给了我这样的信息:
PhalconException: AsdfasdfasdcxccarfController handler class cannot be loaded
这是合乎逻辑的,因为这个控制器不存在.
但是,如何让Phalcon将每个没有控制器的坏URL重定向到我的默认控制器IndexController?
您可以将以下调度程序服务添加到依赖项注入容器.它将检查内部Phalcon错误(例如错误的控制器)并将用户转发到指定的控制器和操作:
$di->set('dispatcher', function() {
$eventsManager = new \Phalcon\Events\Manager();
$eventsManager->attach("dispatch:beforeException", function($event, $dispatcher, $exception) {
//Handle 404 exceptions
if ($exception instanceof \Phalcon\Mvc\Dispatcher\Exception) {
$dispatcher->forward(array(
'controller' => 'index',
'action' => 'show404'
));
return false;
}
//Handle other exceptions
$dispatcher->forward(array(
'controller' => 'index',
'action' => 'show503'
));
return false;
});
$dispatcher = new \Phalcon\Mvc\Dispatcher();
//Bind the EventsManager to the dispatcher
$dispatcher->setEventsManager($eventsManager);
return $dispatcher;
}, true);
Run Code Online (Sandbox Code Playgroud)