1

how to create 404 error page for manual bootstrap for example in this app ? http://album-o-rama.phalconphp.com/

i use this dispatcher :

$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

);

4

4 回答 4

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);
于 2014-06-27T07:31:49.013 回答
1

这里推荐的功能是:

http://docs.phalconphp.com/en/latest/reference/routing.html#not-found-paths

有可能

routing.html#dealing-with-extra-trailing-slashes

对于手动引导,您可以设置路由器,而不是使用调度程序

/**
 * 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错误页面!

仅供评论,我为您提到的应用程序请求此解决方案:

https://github.com/phalcon/album-o-rama/pull/5/files

于 2014-07-01T02:28:44.230 回答
1

对于新版本,Phalcon您可以通过将此代码添加到使用路由来处理错误service.php

$di->set('router',function() use($Config){
    $router = new \Phalcon\Mvc\Router();
    $router->notFound(array(
        "controller" => "error",
        "action" => "error404"
    ));
    return $router;
}); 
于 2015-04-20T13:04:17.027 回答
-2
public function show404Action()
{
    $this->response->setStatusCode(404, 'Not Found');
    $this->view->pick('error/show404');
}
于 2014-06-28T18:07:29.467 回答