如何将随机路由 (404) 定向到 laravel 中的一个视图页面?
How to direct random routing (404) to one view page in laravel?
我只想将随机 link 定向到一个视图,除了我定义的
e.g : localhost:8000/abxcdss
它将转到视图或主页。
解决方案 #1 - 通过异常处理程序 (404)
app/Exceptions/Handler.php
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
//
public function render($request, Exception $exception)
{
if ($exception instanceof NotFoundHttpException) {
return redirect('/');
}
return parent::render($request, $exception);
}
解决方案 #2 - 通过路由
routes/web.php
// Last line!
Route::any('{any}', function () {
return redirect('/');
});
我只想将随机 link 定向到一个视图,除了我定义的
e.g : localhost:8000/abxcdss
它将转到视图或主页。
解决方案 #1 - 通过异常处理程序 (404)
app/Exceptions/Handler.php
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
//
public function render($request, Exception $exception)
{
if ($exception instanceof NotFoundHttpException) {
return redirect('/');
}
return parent::render($request, $exception);
}
解决方案 #2 - 通过路由
routes/web.php
// Last line!
Route::any('{any}', function () {
return redirect('/');
});