我的应用程序有一个登陆页面和一个管理面板。因此,我想为这些视图创建不同的404页。我的views文件夹有两个文件夹:admin和site,其中有一个errors文件夹,其中包含创建的404.blade.php文件。为了达到我的目标,我在app/Exceptions/Handler.php中使用了一个名为app/Exceptions/Handler.php的方法,但不幸的是,它无法工作。解决办法是什么?
这里是renderHttpException(HttpException $e) 方法:
protected function renderHttpException(HttpException $e)
{
$status = $e->getStatusCode();
if(Request::is('/admin/*')) {
return response()->view("admin/errors.{$status}", ['exception' => $e], $status, $e->getHeaders());
}else {
return response()->view("site/errors.{$status}", ['exception' => $e], $status, $e->getHeaders());
}
}和路由:
/* Site routes */
Route::get('/', 'HomeController@index')->name('home');
Route::get('/menu', 'MenuController@index')->name('menu');
/* Admin panel routes */
Route::prefix('/admin')->namespace('Admin')->group(function () {
Route::get('/', 'HomeController@index')->name('admin-dashboard');
Route::get('/login', 'HomeController@showLoginForm')->name('admin-login');
Route::get('/menu', 'MenuController@index')->name('admin-menu');
});结果抛出一个错误:
( App\Exceptions\Handler::renderHttpException(App\Exceptions\HttpException $e)的声明应该与Illuminate\Foundation\Exceptions\Handler::renderHttpException(Symfony\Component\HttpKernel\Exception\HttpException $e兼容)
发布于 2018-02-06 13:48:58
将其放在\App\Exception\Handler::render中
if($exception instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException) {
$view = $request->is('admin/*') ? 'admin/errors.404' : 'site/errors.404' ;
return response()->view($view, [], 404);
}因此,您的方法应该如下所示:
public function render($request, Exception $exception)
{
if($exception instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException) {
$view = $request->is('admin/*') ? 'admin/errors.404' : 'site/errors.404' ;
return response()->view($view, [], 404);
}
$e = $this->prepareException($exception);
if ($e instanceof FlashingException) {
return $e->getResponse();
}
return parent::render($request, $exception);
}https://stackoverflow.com/questions/48643268
复制相似问题