Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Marcolino922's avatar

Rendering Exceptions Redirect

Hi, I have installed Laravel 8 for a project, I also use Spatie for managing roles and permissions.

I was wondering how in the event of an error page, which shows the warning, instead redirect the user to the home by showing a simple alert.

I find it unpleasant to show the error on a single page, it would be more appropriate for me to show the error with an alert directly on the home page.

Thanks to anyone who will help me.

0 likes
6 replies
lacasera's avatar

@marcolino922 are you using a custom exception to handle the error you want to track? anyway you would have to do something like this

 // handle redirection to homepage. (replace /home with the homepage route
  return redirect()->to('/home')->with('error', 'this is an error message');


//home.blade.php

@if (session('error'))
    <div class="alert alert-error">
        {{ session('error') }}
    </div>
@endif
Marcolino922's avatar

Hi, thanks for your help but sadly that's not what I'm looking for. In the sense, what I would need concerns certain exceptions, such as errors 500, 503, 404, 403, 401 which are shown on a separate page. I would like the message to be shown in home with a redirect instead.

I believe I should act on app/Exceptions/Handler? But how?

1 like
lacasera's avatar

then this is what you need.

//app/Exceptions/Handler

 public function render($request, Throwable $e)
 {
        if (get_class($e) === \Symfony\Component\HttpKernel\Exception\NotFoundHttpException::class){
            return redirect()->to(route('home'))->with('error', 'this is an error');
        }
        return parent::render($request, $e); // TODO: Change the autogenerated stub
    }


//home.blade.php
@if (session('error'))
    <div class="alert alert-error">
        {{ session('error') }}
    </div>
@endif

NB. this is for 404 errors.

the other status codes with throw \Symfony\Component\HttpKernel\Exception\HttpException exceptions

1 like
MichalOravec's avatar

This is default renderHttpException from Illuminate\Foundation\Exceptions\Handler

use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;

/**
 * Render the given HttpException.
 *
 * @param  \Symfony\Component\HttpKernel\Exception\HttpExceptionInterface  $e
 * @return \Symfony\Component\HttpFoundation\Response
 */
protected function renderHttpException(HttpExceptionInterface $e)
{
    $this->registerErrorViewPaths();

    if (view()->exists($view = $this->getHttpExceptionView($e))) {
        return response()->view($view, [
            'errors' => new ViewErrorBag,
            'exception' => $e,
        ], $e->getStatusCode(), $e->getHeaders());
    }

    return $this->convertExceptionToResponse($e);
}

You have to modify this method in app/Exceptions/Handler.php

Marcolino922's avatar

I did some tests, but either it stays the same or the page is not reachable.

Please or to participate in this conversation.