[英]laravel if route is null or not existing redirect to other page
[英]Laravel Redirect not existing Route
我在其render()
方法的App/Exceptions/Handler.php
中有以下代码:
if ($this->isHttpException($e)) {
switch ($e->getStatusCode()) {
// not found
case '404';
return redirect()->route('index');
break;
// internal error
case '500';
return redirect()->route('index');
break;
default;
return $this->renderHttpException($e);
break;
}
} else {
return parent::render($request, $e);
}
但是问题是当我转到localhost:8000/vendor
,我没有被重定向。 我得到以下回应:
在此服务器上找不到请求的资源/供应商。
我希望将导致“ 404未找到”的所有URL重定向到我的主页。
问题在于某些软件包将资产发布到public/vendor
,因此它是一个文件夹,并且取决于您的Web服务器的设置,该文件夹可能会或可能无法列出(某些Web服务器的Options +Indexes
)。 在大多数情况下,不允许列出其内容,因此您会看到404 Not Found
或403 Forbidden
。
要在Apache中重定向,请将以下内容放在位于public/
中的.htaccess
文件中。
ErrorDocument 404 https://your-website.com/
如果您使用的是nginx,请在配置文件中放置:
error_page 404 /;
对于内置的PHP服务器( php artisan serve
eq),您应该以某种方式手动进行操作……希望,您仅将其用于应用程序开发和生产,所以没关系。
我将在路由文件中使用404路由来处理它( 我想捕获路由中不存在的所有路由。我希望将它们重定向到我的主页。 )
Route::fallback(function ()
{
# To Specific Controller
return Redirect::to('homeController'); # ('/') if defined
# To Specific View
return response()->view('custom.file.path', [], 404);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.