php 如果在 Laravel 5.1 中找不到路由,则显示 404 页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32195346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Show a 404 page if route not found in Laravel 5.1
提问by scott
I am trying to figure out to show 404 page not found if a route is not found. I followed many tutorials, but it doesn't work.
I have 404.blade.php
in \laravel\resources\views\errors
我想弄清楚如果找不到路线,则显示 404 页面未找到。我跟着很多教程,但它不起作用。我404.blade.php
在\laravel\resources\views\errors
Also in handler.php
也在 handler.php 中
public function render($request, Exception $e)
{
if ($e instanceof TokenMismatchException) {
// redirect to form an example of how i handle mine
return redirect($request->fullUrl())->with(
'csrf_error',
"Opps! Seems you couldn't submit form for a longtime. Please try again"
);
}
/*if ($e instanceof CustomException) {
return response()->view('errors.404', [], 500);
}*/
if ($e instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException)
return response(view('error.404'), 404);
return parent::render($request, $e);
}
If I enter wrong URL in browser, it returns a blank page. I have
如果我在浏览器中输入错误的 URL,它会返回一个空白页面。我有
'debug' => env('APP_DEBUG', true),
in app.php.
在 app.php 中。
Can anyone help me how to show a 404 page if route is not found? Thank you.
如果找不到路线,谁能帮助我如何显示 404 页面?谢谢你。
回答by Pim
I recieved 500 errors instead of 404 errors. I solved the problem like this:
我收到了 500 个错误而不是 404 个错误。我解决了这样的问题:
In the app/Exceptions/Handler.php file, there is a renderfunction.
在 app/Exceptions/Handler.php 文件中,有一个渲染函数。
Replace the function with this function:
用这个函数替换这个函数:
public function render($request, Exception $e)
{
if ($this->isHttpException($e)) {
switch ($e->getStatusCode()) {
// not authorized
case '403':
return \Response::view('errors.403',array(),403);
break;
// not found
case '404':
return \Response::view('errors.404',array(),404);
break;
// internal error
case '500':
return \Response::view('errors.500',array(),500);
break;
default:
return $this->renderHttpException($e);
break;
}
} else {
return parent::render($request, $e);
}
}
You can then use views that you save in views/errors/404.blade.php, and so on.
然后,您可以使用保存在 views/errors/404.blade.php 中的视图,依此类推。
回答by mdamia
> The abort method will immediately raise an exception which will be rendered by the exception handler. Optionally, you may provide the response text:
> abort 方法将立即引发一个异常,该异常将由异常处理程序呈现。或者,您可以提供响应文本:
abort(403, 'Unauthorized action.');
is your app_debug set to true? if that is the case, Laravel will throw the error with backtrace for debugging purposes, if you change the value to false, Laravel will show the default 404 page in the errors folder. That being said you can choose to use abort at any time you want. at the controller level or at the route level, it is totally up to you.
你的 app_debug 设置为 true 了吗?如果是这种情况,Laravel 将使用 backtrace 抛出错误以进行调试,如果您将值更改为 false,Laravel 将在错误文件夹中显示默认的 404 页面。也就是说,您可以随时选择使用 abort。在控制器级别或路由级别,这完全取决于您。
ie
IE
Route::get('/page/not/found',function($closure){
// second parameter is optional.
abort(404,'Page not found');
abort(403);
});
回答by mdamia
@tester.Your problem has already been solved, try the command below in composer:
@tester.你的问题已经解决了,在composer中试试下面的命令:
php artisan view:clear
Then try once more with an unknown URL. Because I have also faced the same error before.
然后使用未知 URL 再试一次。因为我之前也遇到过同样的错误。
回答by patricus
There is no need for you to check the error type and manually render the 404 view. Laravel already knows to render the view with the HTTP error code that was thrown (404 = resources/views/errors/404.blade.php). Get rid of the extra check and it should work fine.
您无需检查错误类型并手动呈现 404 视图。Laravel 已经知道使用抛出的 HTTP 错误代码渲染视图(404 = resources/views/errors/404.blade.php)。摆脱额外的检查,它应该可以正常工作。
public function render($request, Exception $e)
{
if ($e instanceof TokenMismatchException) {
// redirect to form an example of how i handle mine
return redirect($request->fullUrl())->with(
'csrf_error',
"Opps! Seems you couldn't submit form for a longtime. Please try again"
);
}
return parent::render($request, $e);
}
回答by Shinseiki86
I use the following in app/Exceptions/Handler.php (Laravel 5.2):
我在 app/Exceptions/Handler.php (Laravel 5.2) 中使用以下内容:
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $e
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $e)
{
if ($e instanceof \ReflectionException OR $e instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException) //Si la ruta no existe, mostar view 404.
return response(view('errors.404'), 404);
return parent::render($request, $e);
}
And it looks like this:img
它看起来像这样:img
回答by Mohammed Elhag
In apache you could be able to put this code in .htaccess file at your main directory and make sure that change AllowOverride Directive to all in httpd confg file
在 apache 中,您可以将此代码放在主目录的 .htaccess 文件中,并确保在 httpd 配置文件中将 AllowOverride Directive 更改为 all
ErrorDocument 404 the\path\to4.blade.php