【问题标题】:Phalcon: not found page error handlerPhalcon:未找到页面错误处理程序
【发布时间】:2014-06-27 07:26:54
【问题描述】:

如何为手动引导创建 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

);

【问题讨论】:

    标签: php phalcon phalcon-routing


    【解决方案1】:

    在你的 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);
    

    【讨论】:

    • 当使用这个调度器时也会显示这个错误:Service 'view' was not found in the dependency injection container
    【解决方案2】:

    这里推荐的功能是:

    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

    【讨论】:

    • 如果 url 部分正确,即 /valid/invalid 则不起作用
    【解决方案3】:

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

    $di->set('router',function() use($Config){
        $router = new \Phalcon\Mvc\Router();
        $router->notFound(array(
            "controller" => "error",
            "action" => "error404"
        ));
        return $router;
    }); 
    

    【讨论】:

      【解决方案4】:
      public function show404Action()
      {
          $this->response->setStatusCode(404, 'Not Found');
          $this->view->pick('error/show404');
      }
      

      【讨论】:

      • 答案与解释它们的文字配合得更好。解释有助于学习。因此,这个答案实际上出现在低质量帖子的审核队列中。
      猜你喜欢
      • 1970-01-01
      • 2016-05-05
      • 2012-09-10
      • 1970-01-01
      • 1970-01-01
      • 2015-06-22
      • 1970-01-01
      • 2019-09-21
      • 2012-09-15
      相关资源
      最近更新 更多