0
votes

Suppose I have this in my routes.php :

Route::get('sells', ['as' => 'user_sells', 'uses' => 'SellsController@indexAll']);

Now, as you know, if someone opens mySite.com/sells, the desired method in the controller would be executed.

But, if someone tried to access to a route which is not defined at all, like mySite.com/buys , I want to show a default error page. I mean, I need to say if a route is not defined, show a specific page.

How can I do that?

Thanks in advance

Added: The error I face to when I try to access an undefined route:

Whoops, looks like something went wrong.

ErrorException in C:\wamp\www\codes\laravel5\portpapa\vendor\laravel\framework\src\Illuminate\Container\Container.php line 835: Unresolvable dependency resolving [Parameter #0 [ $methods ]] in class Illuminate\Routing\Route (View: ...

2

2 Answers

3
votes

Actually, Laravel already have this by default. If you create a view called errors/404.blade.php in the resources/views folder, this will be automatic.

If you want to handle the 404 error with custom code, just catch the NotFoundHttpException exception in the App\Exceptions\Handler class:

public function render($request, Exception $e)
{
    if ($e instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException) {
        // handle here
        return response()->view('errors.404', [], 404);
    }
}
3
votes

If the route is not defined, the NotFoundHttpException will be throw. The exceptions are managed in Larevel in app/Exceptions/handler.php.

You have to check if the exception is NotFoundHttpException and, in this case, return the correct view.

public function render($request, Exception $e)
{
    if ($this->isHttpException($e))
    {       
        if($e instanceof NotFoundHttpException)
        {
            return response()->view('my.view', [], 404);
        }
        return $this->renderHttpException($e);
    }
    return parent::render($request, $e);
}

Source.