Skumar
Skumar

Reputation: 520

Laravel 8 - Show a customised 404 blade page, when a Controller does not exist

When there is no controller action , then it can be checked as

if(! method_exists($controller, $action)) {
    return 404;
}

But when there is no Controller, I am getting this error

Illuminate\Contracts\Container\BindingResolutionException
Target class [\App\Http\Controllers\DashboardController] does not exist.

But, is it possible to show a custom 404 blade page instead?

Upvotes: 2

Views: 4331

Answers (6)

Tanner
Tanner

Reputation: 868

So all of the default Laravel errors pages can be found ./vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/views

You can run this command to publish the error view pages yourself and customize them. php artisan vendor:publish --tag=laravel-errors Then they will be located ./resources/views/errors

Also, you should use abort(404) instead of returning 404. This will fire the render in your App\Exceptions\Handler If you did not overwrite this method it will use the Laravel default. But to overwrite you could do

app/Exceptions/Handler.php

public function render($request, Throwable $exception)

{
    $response = parent::render($request, $exception);

    if ($response->status() === 404) {
        // return something custom
    }

    return $response;
}

You can follow the documentation here https://laravel.com/docs/8.x/errors#custom-http-error-pages. Hope this helps!

Upvotes: 4

pc_fuel
pc_fuel

Reputation: 786

As @Droid pointed out,

  • set APP_DEBUG=false in the .env file.

  • Then use php artisan vendor:publish --tag=laravel-errors in command prompt (where the project is stored). All the Laravel designed error pages (40x,50x, etc.) will be shown.

  • In here, customise these pages as per your requirement.

From then, irrespective of any error message, whether you mess up with your URL or something happens on the server side, you will see your customized error pages.

Upvotes: 2

P. K. Tharindu
P. K. Tharindu

Reputation: 2730

4XX response status codes represent "Client errors". 404 Not Found should be thrown when the server can not find the requested resource.

On the other hand, 5XX response status codes represent "Server errors". 500 Internal Server Error means the server has encountered a situation it doesn't know how to handle.

The error you are getting is actually a server error.

It has nothing to do with the client and therefore, it should not be throwing 404 Not Found.

So instead, you can just create the controller and do abort(404); inside the controller method like so:

class DashboardController extends Controller
{
    public function index()
    {
        abort(404);
    }
}

Resource: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status

Upvotes: 1

Ali Abbasov
Ali Abbasov

Reputation: 113

You can try creating a folder called errors in your views folder. Then create another view called 404.blade.php but inside errors folder. Laravel will understand that the 404 blade file simply represents the page where you want to redirect when people are getting 404 exception.

So after that you can customize 404 blade file as you wish and whenever you get 404 error that page will show up

Upvotes: 1

Kamlesh Paul
Kamlesh Paul

Reputation: 12391

you can customise 404 error by publishing laravel errors page and customise that

Run

php artisan vendor:publish --tag=laravel-errors

it be publish all the errors page laravel have in views/errors dir

ref link

https://laravel.com/docs/8.x/errors#http-exceptions

Upvotes: 1

New Bee 1
New Bee 1

Reputation: 75

Try this

Route::any('/', function () {
    //
});

for unknown controller and make a view 


[documentation][1] :https://laravel.com/docs/8.x/routing

Upvotes: 1

Related Questions