[英]Phalcon: not found page error handler
如何在此應用程序中創建404錯誤頁面以進行手動引導? http://album-o-rama.phalconphp.com/
我使用這個調度程序:
$di->set(
'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' => 'error',
'action' => 'show404',
)
);
return false;
}
}
);
$dispatcher = new PhDispatcher();
$dispatcher->setEventsManager($evManager);
return $dispatcher;
},
true
);
在index.php中嘗試一下:
$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);
這里推薦的功能是:
http://docs.phalconphp.com/zh_CN/latest/reference/routing.html#not-found-paths
有可能
routing.html#dealing-with-extra-trailing-斜杠
對於手動引導,可以使用路由器來代替使用調度程序
/**
* Registering a router
*/
$di->set('router', require __DIR__.'/../common/config/routes.php');
然后在“ common / config / routes.php”中添加此路由規則。
$router->notFound(array(
'module' => 'frontend',
'namespace' => 'AlbumOrama\Frontend\Controllers\\',
'controller' => 'index',
'action' => 'route404'
));
最后定義一個控制器和一個視圖以捕獲此操作。
瞧!404錯誤頁面!
僅作評論,我要求為您提到的應用請求此解決方案:
對於新版本的Phalcon
您可以通過將此代碼添加到service.php
來使用路由來處理錯誤service.php
$di->set('router',function() use($Config){
$router = new \Phalcon\Mvc\Router();
$router->notFound(array(
"controller" => "error",
"action" => "error404"
));
return $router;
});
public function show404Action()
{
$this->response->setStatusCode(404, 'Not Found');
$this->view->pick('error/show404');
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.