2

My application has a landing page and an admin panel. So, I want to create different 404 pages for those views. My views folder has 2 folders: admin and site and in them I have errors folder with created 404.blade.php files. In order to achieve my aim I've overrided a method called renderHttpException(HttpException $e) in app/Exceptions/Handler.php but unfortunately it doesn't work. What is the workaround?

Here's the renderHttpException(HttpException $e) method:

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());
    }    
}

And also routes:

/* 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');
});

It throws an error as a result:

Declaration of App\Exceptions\Handler::renderHttpException(App\Exceptions\HttpException $e) should be compatible with Illuminate\Foundation\Exceptions\Handler::renderHttpException(Symfony\Component\HttpKernel\Exception\HttpException $e)

9
  • Is this working dd($request->route()->getPrefix()); ? (Adapt it to use the facade) If yes then change if(Request::is('/admin/*')) {... with if($request->route()->getPrefix() == '/admin') Commented Feb 6, 2018 at 12:40
  • @CalinBlaga Where should I put it? Commented Feb 6, 2018 at 12:43
  • inside renderHttpException() Commented Feb 6, 2018 at 12:44
  • @CalinBlaga Please look at the question, I updated. It throws an error... As a result your code given above isn't working. Commented Feb 6, 2018 at 12:47
  • laravel version? Commented Feb 6, 2018 at 12:49

1 Answer 1

3

Place this in \App\Exceptions\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);
}

so your method should look like this:

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);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.