4

I would like to show 404 page in Laravel 5 while MethodNotAllowedHttpException throws.

Can anyone help me in this regard?

Script47
  • 14,230
  • 4
  • 45
  • 66
julious ceazer
  • 331
  • 2
  • 4
  • 14

4 Answers4

12

Add this to app/Exceptions/Handler.php:

public function render($request, Exception $e) 
{
    if ($e instanceof MethodNotAllowedHttpException) 
    {
        abort(404);
    }
    return parent::render($request, $e);
}

Then edit resource/views/errors/404.blade.php to personalize the page.

Burgi
  • 421
  • 8
  • 24
Bruno Lebtag
  • 420
  • 4
  • 18
  • 2
    Remember to add `use \Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException` at the start of the file if you don't want to use the full namespace – Nil Llisterri Nov 28 '16 at 09:55
3

Simple: all you have to do is create a template at resources/views/errors/404.blade.php.

You can create views for other HTTP status codes if you’re feeling that way inclined, such as a 403.blade.php for Forbidden exceptions and so on.

Martin Bean
  • 38,379
  • 25
  • 128
  • 201
  • 1
    OP is asking how to display a 404 page when a specific error is thrown, not how to edit the template for the 404 error itself. – Fuseblown Jun 02 '15 at 19:10
1

I'm working on Laravel 5.2 and the answer given here worked for me.

You need to modify the render method in the app/Exceptions/Handler.php.

    public function render($request, Exception $e)
    {
        if ($e instanceof \Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException) 
        {
            return response(view('errors.404'), 404);
        }

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

abort(404) did not work for me. Looks like this method is removed in Laravel 5.

Community
  • 1
  • 1
Raghav
  • 265
  • 1
  • 5
  • 11
0

That's a 405 error, easiest way would be to create a new view in resources/views/errors/405.blade.php..

If you need fine control over what is displayed then you'll have to use the Handler.php to modify what's returned.

mylesthe.dev
  • 9,565
  • 4
  • 23
  • 34