【问题标题】:laravel routing and 404 errorlaravel 路由和 404 错误
【发布时间】:2013-12-26 19:04:03
【问题描述】:

我想指定是否在 url 地址中输入了除现有路由以外的任何内容(在 routes.php 中,然后显示 404 页面。

我知道这件事:

App::abort(404);

但是我怎样才能指定除了定义的路由之外的所有部分?

【问题讨论】:

    标签: routing laravel laravel-4


    【解决方案1】:

    您可以将其添加到您的 filters.php 文件中:

    App::missing(function($exception)
    {
        return Response::view('errors.missing', array(), 404);
    });
    

    并创建errors.missing 视图文件以向他们显示错误。

    也看看Errors & Logging docs

    编辑

    如果您需要将数据传递给该视图,则第二个参数是您可以使用的数组:

    App::missing(function($exception)
    {
        return Response::view('errors.missing', array('url' => Request::url()), 404);
    });
    

    【讨论】:

    • 如何将 Request:url() 传递给 missign Blade。
    • 编辑回答您的问题。
    • 你好...可以获取上一个url在404页面中添加一个链接比如“Go to previous page”吗?
    • 我正在使用Laravel 5.2,而filters.php 不可用。你能建议我应该怎么做吗?
    • @Helper 在 Laravel 5 中,你可以打开你的 app/Http/Exceptions/Handler.php 文件,然后在 render() 方法中,添加这个检查:if ($e->getStatusCode() == 404) { return response()->view('errors.customView', ['data' => 'custom_data'], $e->getStatusCode()); }
    【解决方案2】:

    我建议把它放到你的 app/start/global.php 中,因为这是 Laravel 默认处理它的地方(尽管 filters.php 也可以工作)。我通常使用这样的东西:

    /*
    |--------------------------------------------------------------------------
    | Application Error Handler
    |--------------------------------------------------------------------------
    |
    | Here you may handle any errors that occur in your application, including
    | logging them or displaying custom views for specific errors. You may
    | even register several error handlers to handle different types of
    | exceptions. If nothing is returned, the default error view is
    | shown, which includes a detailed stack trace during debug.
    |
    */
    
    App::error(function(Exception $exception, $code)
    {
        $pathInfo = Request::getPathInfo();
        $message = $exception->getMessage() ?: 'Exception';
        Log::error("$code - $message @ $pathInfo\r\n$exception");
    
        if (Config::get('app.debug')) {
            return;
        }
    
        switch ($code)
        {
            case 403:
                return Response::view('errors/403', array(), 403);
    
            case 500:
                return Response::view('errors/500', array(), 500);
    
            default:
                return Response::view('errors/404', array(), $code);
        }
    });
    

    然后只需在/views 中创建一个errors 文件夹并将错误页面内容放在那里。正如 Antonio 所说,您可以在 array() 中传递数据。

    我从https://github.com/andrewelkins/Laravel-4-Bootstrap-Starter-Site借用了这个方法

    【讨论】:

    • 无法使用Laravel 5.2app/start/global.php。你能建议我应该怎么做吗?
    【解决方案3】:

    对于使用 Laravel 5 的人来说,views 目录中有一个 errors 文件夹。您只需要在那里创建一个 404.blade.php 文件,当没有为 url 指定路由时,它将呈现此视图

    【讨论】:

      【解决方案4】:

      这是我仅用于使用模板布局显示错误 404 的方法。只需将以下代码添加到/app/start/global.php 文件中

      App::missing(function($exception)
      {
          $layout = \View::make('layouts.error');
          $layout->content = \View::make('views.errors.404');
          return Response::make($layout, 404);
      });
      

      【讨论】:

        猜你喜欢
        • 2018-01-15
        • 2014-12-21
        • 2017-10-20
        • 2018-01-18
        • 2016-01-12
        • 2018-09-26
        • 1970-01-01
        • 2015-02-04
        • 1970-01-01
        相关资源
        最近更新 更多