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

onurzdgn's avatar

Laravel erro handling 10 to 11

Hello everyone, I am using Laravel 11 and I want to error handling. I was did in Laravel 10 in app/Exceptions/Handler.php. However there is no handler.php in Laravel 11. How can I create separate custom error pages. For example admin panel have different 404 page from customer 404 page. I was did in Laravel 10 in this style:

        if ($exception instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException) {
            if ($request->is(['admin', 'admin/*'])) {
                if ($exception->getStatusCode() == 400) {
                    return response()->view("admin.errors.400", [], 400);
                }

                if ($exception->getStatusCode() == 403) {
                    return response()->view("admin.errors.403", [], 403);
                }

                if ($exception->getStatusCode() == 404) {
                    return response()->view("admin.errors.404", [], 404);
                }

                if ($exception->getStatusCode() == 500) {
                    return response()->view("admin.errors.500", [], 500);
                }

                if ($exception->getStatusCode() == 503) {
                    return response()->view("admin.errors.503", [], 503);
                }
            }

            if ($exception->getStatusCode() == 404) {
                return response()->view("customer.errors.404", [], 404);
            }
        }
0 likes
1 reply
onurzdgn's avatar
onurzdgn
OP
Best Answer
Level 2

use Illuminate\Http\Request;
use \Symfony\Component\HttpKernel\Exception\NotFoundHttpException;


$exceptions->render(function (NotFoundHttpException $exception, Request $request) {
            if ($request->is(['admin', 'admin/*'])) {
                if ($exception->getStatusCode() == 400) {
                    return response()->view("admin.errors.400", [], 400);
                }

                if ($exception->getStatusCode() == 403) {
                    return response()->view("admin.errors.403", [], 403);
                }

                if ($exception->getStatusCode() == 404) {
                    return response()->view("admin.errors.404", [], 404);
                }

                if ($exception->getStatusCode() == 500) {
                    return response()->view("admin.errors.500", [], 500);
                }

                if ($exception->getStatusCode() == 503) {
                    return response()->view("admin.errors.503", [], 503);
                }
            }

            if ($exception->getStatusCode() == 404) {
                return response()->view("customer.errors.404", [], 404);
            }
        });

from https://stackoverflow.com/a/78580748/19671809

Please or to participate in this conversation.