简体   繁体   English

在 laravel 5.4 自动重定向到 404 页面

[英]redirect to 404 page automatically at laravel 5.4

i made simple resume site with laravel 5.4 and i wanna if users type anything except my site name and my site.com/panel automatically redirect to 404 page.我用 laravel 5.4 制作了简单的简历网站,如果用户输入除我的网站名称和我的 site.com/panel 之外的任何内容,我想自动重定向到 404 页面。

how can i do that?我怎样才能做到这一点?

is there any route to do that or what?有没有办法做到这一点或什么?

i found this code but not use我找到了这段代码但没有使用

 public function render($request, Exception $e)
{
    if ($e instanceof 
    \Symfony\Component\HttpKernel\Exception\NotFoundHttpException){

        return response(redirect(url('/')), 404);
    }
    return parent::render($request, $e);

}

just add abort method只需添加abort方法

 return abort(404);

it's automatically redirect to your resources/views/errors/404.blade.php它会自动重定向到您的resources/views/errors/404.blade.php

return abort(404);

并通过获取请求为这个特定的操作/方法设置你的路线。

此外,您可以提供响应文本:

return abort(403, 'Unauthorized action.');

you can create a view in您可以在

resources/views/errors/404.blade.php

and it will redirect every unexist route that you didn't include it in它将重定向您未包含在其中的每条不存在的路线

 web.php

and in 404.blade file you can put this hyper link to redirect use to your home page either by choosing route name在 404.blade 文件中,您可以通过选择路由名称将此超链接重定向到您的主页

<a href="{{ route('home') }}">Back to home</a>

or by specifying a route link directly或直接指定路由链接

<a href="/">Back to home</a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM