如何在手动引导程序中创建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
);
答案 0 :(得分:4)
在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);
答案 1 :(得分:1)
这里推荐的功能是:
http://docs.phalconphp.com/en/latest/reference/routing.html#not-found-paths
也许
routing.html#处理与 - 外拖尾斜线
对于手动引导程序,您可以设置路由器
,而不是使用dispatcher/**
* 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'
));
最后定义一个控制器和一个视图来捕获这个动作。
并且voilà:404错误页面!
仅仅是为了评论,我为您提到的应用程序请求此解决方案:
答案 2 :(得分:1)
对于Phalcon
的新版本,您可以通过将此代码添加到service.php
$di->set('router',function() use($Config){
$router = new \Phalcon\Mvc\Router();
$router->notFound(array(
"controller" => "error",
"action" => "error404"
));
return $router;
});
答案 3 :(得分:-2)
public function show404Action()
{
$this->response->setStatusCode(404, 'Not Found');
$this->view->pick('error/show404');
}