【问题标题】:How to add a 404 error code when route does not exisit?路由不存在时如何添加 404 错误代码?
【发布时间】:2012-09-17 05:39:12
【问题描述】:

当路由不存在时如何抛出 404 错误代码?

在您设置路由信息后在 phalcon 中 - 有没有办法检查进入(来自用户)的路由是否与您的路由列表中的任何路由匹配?然后,如果路由不存在,则抛出 404 错误。

【问题讨论】:

    标签: php routing http-status-code-404 phalcon


    【解决方案1】:

    设置 404 页面后,您只需将访问者发送到此页面的错误网址即可。为此,只需将以下行添加到您的 .htaccess 文件中:

    ErrorDocument 404 /404.php
    

    您可以将 404 错误模板放置在您想要的任何位置。例如,您可以将所有错误消息放在名为 errormessages 的文件夹中

     ErrorDocument 404 /errormessages/404.php 
    

    【讨论】:

      【解决方案2】:

      如果您想在执行 PHP 脚本时使用默认的 HTTP 404 错误:

      header("HTTP/1.0 404 Not Found");
      

      但请记住,这是您发送给客户的第一件事。

      如果您不想自定义 Web 服务器默认的 PHP 错误,这取决于软件。很容易找到 filde apache 用于显示此类错误的示例。

      【讨论】:

        【解决方案3】:

        你可以这样使用:

        public function main()
        {
            try {
        
                $this->_registerServices();
                $this->registerModules(self::$modules);
                $this->handle()->send();
        
            } catch (Exception $e) {
        
                // TODO log exception
        
                // remove view contents from buffer
                ob_clean();
        
                $errorCode = 500;
                $errorView = 'errors/500_error.phtml';
        
                switch (true) {
                    // 401 UNAUTHORIZED
                    case $e->getCode() == 401:
                        $errorCode = 401;
                        $errorView = 'errors/401_unathorized.phtml';
                        break;
        
                    // 403 FORBIDDEN
                    case $e->getCode() == 403:
                        $errorCode = 403;
                        $errorView = 'errors/403_forbidden.phtml';
                        break;
        
                    // 404 NOT FOUND
                    case $e->getCode() == 404:
                    case ($e instanceof Phalcon\Mvc\View\Exception):
                    case ($e instanceof Phalcon\Mvc\Dispatcher\Exception):
                        $errorCode = 404;
                        $errorView = 'errors/404_not_found.phtml';
                        break;
                }
        
                // Get error view contents. Since we are including the view
                // file here you can use PHP and local vars inside the error view.
                ob_start();
                include_once $errorView;
                $contents = ob_get_contents();
                ob_end_clean();
        
                // send view to header
                $response = $this->getDI()->getShared('response');
                $response->resetHeaders()
                    ->setStatusCode($errorCode, null)
                    ->setContent($contents)
                    ->send();
            }
        }
        

        如果你正在使用Micro 组件,你可以使用这个:

        $app->notFound(
            function () use ($app) {
                $app->response->setStatusCode(404, "Not Found")->sendHeaders();
                echo 'This is crazy, but this page was not found!';
            }
        );
        

        当然,您可以使用其他人发布的有关 .htaccess 文件的建议,但以上是您在 Phalcon 中的操作方法,无需触及其他任何内容。

        还有一个新功能即将推出,关于一个默认错误处理程序,它将处理 Phalcon 中的错误(或在必要时覆盖)。

        感谢 Nesbert 的 gist

        【讨论】:

        • 这似乎是我需要的,让我试试看。感谢您的快速回复。
        • 谢谢,它成功了。我必须进行一些更改,以便它可以与我拥有的多模型文件结构一起使用。 docs.phalconphp.com/en/0.5.0/reference/…
        猜你喜欢
        • 2021-02-21
        • 1970-01-01
        • 2019-04-20
        • 2020-02-05
        • 1970-01-01
        • 2018-01-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多