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

jlmmns's avatar
Level 12

How to change the base path for error pages triggered by a route?

Hi all,

The default path for error pages is "resources/views/errors".
Is there a way to customize this path according to route groups?

I want different error pages for my frontend, as well as my backend.
So a 404 error page needs to be styled differently from my frontend, when inside my backend.

Basically, I want to dynamically update the base errors path, when inside a route group.

Something like this:

Route::group([ 'prefix' => 'app' ], function()
{
    $path_errors = "resources/views/app/errors";
}
0 likes
6 replies
jlmmns's avatar
Level 12

@RachidLaasri Thanks Rachid!

Had to overwrite another parent method to get it working.
But isn't this bad practice for when you need to update the Laravel framework?

Is there maybe a better/safer way of doing this?

private $view_errors_path = 'errors';

public function render($request, Exception $e)
{
    if ($e instanceof ModelNotFoundException) {
        $e = new NotFoundHttpException($e->getMessage(), $e);
    }

    if ($request->is('app/*'))
    {
        $this->view_errors_path = 'app.errors';
    }

    return parent::render($request, $e);
}

protected function renderHttpException(HttpException $e)
{
    $status = $e->getStatusCode();

    if (view()->exists("{$this->view_errors_path}.{$status}")) {
        return response()->view("{$this->view_errors_path}.{$status}", ['exception' => $e], $status);
    } else {
        return $this->convertExceptionToResponse($e);
    }
}
RachidLaasri's avatar

The only things that gets replaced when running composer update is the vendor folder, as long as you don't change something in there you are fine.

jlmmns's avatar
Level 12

@RachidLaasri Right, I know that. But what if the overwritten method changes inside vendor?
Then my overwritten method could cause errors, right?

I think the only proper way of changing these paths is for Laravel to have them in a config file.
Where is the best place I can post suggestions for the Laravel framework?

RachidLaasri's avatar

Yeah, and that's normal, you always have to check if there is something you need to change when a new version comes about.

In your case, i believe you don't have to override that method at all. You can do something like this :

/**
 * Render an exception into an HTTP response.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Exception  $e
 * @return \Illuminate\Http\Response
 */
public function render($request, Exception $e)
{
    if ($e instanceof NotFoundHttpException) {
        if($request->is('app/*')) {
            return response()->view("app.errors.404");
        }
        return response()->view("errors.404");
    }

    return parent::render($request, $e);
}
RachidLaasri's avatar

Or even something like this would work:

/**
 * Render an exception into an HTTP response.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Exception  $e
 * @return \Illuminate\Http\Response
 */
public function render($request, Exception $e)
{
    if ($e instanceof NotFoundHttpException) {
        
        $view = $request->is('app/*') ? 'app.errors.404' : 'errors.404';
        
        return response()->view($view);
    }

    return parent::render($request, $e);
}

Please or to participate in this conversation.