laravel路由和404错误

spa*_*key 7 routing laravel laravel-4

我想指定如果在url地址中输入除现有路由之外的任何内容(在routes.php中,则显示404页面.

我知道这件事:

App::abort(404);
Run Code Online (Sandbox Code Playgroud)

但是如何指定除路径定义以外的所有其他部分?

Ant*_*iro 30

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

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

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

另请参阅错误和记录文档

编辑

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

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


Jos*_*ain 5

我建议把它放到你的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);
    }
});
Run Code Online (Sandbox Code Playgroud)

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

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