我想将 Laravel 5.2 版的默认 404 页面重定向到我的 404 模板页面,从哪里以及要更改的内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35837214/
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
I want to redirect Laravel version 5.2 default 404 page to my template page of 404, from where and what to change
提问by Abid Ali
I am using Laravel version 5.2 and don't know how to redirect Laravel default page to my template 404 page
我使用的是 Laravel 5.2 版,但不知道如何将 Laravel 默认页面重定向到我的模板 404 页面
回答by Jim M
use abort(404);
用 abort(404);
Some exceptions describe HTTP error codes from the server. For example, this may be a "page not found" error (404), an "unauthorized error" (401) or even a developer generated 500 error. In order to generate such a response from anywhere in your application, use the following:
abort(404);
一些异常描述了来自服务器的 HTTP 错误代码。例如,这可能是“找不到页面”错误(404)、“未经授权的错误”(401)甚至是开发人员生成的 500 错误。为了从应用程序的任何地方生成这样的响应,请使用以下命令:
abort(404);
If you invoke abort(404);
anywhere in your route or controller it will throw HTTPNotFoundException
which looks for a blade template to display in resources/views/errors/
directory with the filename same as the error code.
如果您abort(404);
在路由或控制器中的任何位置调用它,它将抛出HTTPNotFoundException
寻找刀片模板以显示在resources/views/errors/
文件名与错误代码相同的目录中。
Example:
例子:
in your app/Http/routes.php
在你的 app/Http/routes.php
Route::get('/test', function(){
return abort(404);
});
in your resources/views/errors/
directory create 404.blade.php, notice the name of the file corresponds with the abort(404);
在您的resources/views/errors/
目录中创建404.blade.php,注意文件名与 abort( 404)对应;
Reference: https://laravel.com/docs/5.2/errors#http-exceptions
回答by Pawan Verma
Modify app/Exceptions/Handler.php and add some lines as below:-
修改 app/Exceptions/Handler.php 并添加如下几行:-
public function render($request, Exception $e)
{
if ($e instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException)
{
return redirect('/');
}
return parent::render($request, $e); }
Use your route name in place of / where you want to redirect.
使用您的路线名称代替 / 您要重定向的位置。
回答by Arash Hatami
Create a view and set this code in app/Exceptions/Handler.php:
创建一个视图并在app/Exceptions/Handler.php 中设置此代码:
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
.
.
.
public function render($request, Exception $exception)
{
if($exception instanceof NotFoundHttpException)
{
return response()->view('missing', [], 404);
}
return parent::render($request, $e);
}
回答by andylondon
in the /resources/views/errors directory there is a 404 page
在 /resources/views/errors 目录中有一个 404 页面
edit this page and you can make it look like you want
编辑这个页面,你可以让它看起来像你想要的