4

I am using Laravel version 5.2 and don't know how to redirect Laravel default page to my template 404 page

Laerte
  • 7,013
  • 3
  • 32
  • 50
Abid Ali
  • 1,327
  • 2
  • 10
  • 8
  • Possible duplicate of [Laravel: How to respond with custom 404 error depending on route](http://stackoverflow.com/questions/17972276/laravel-how-to-respond-with-custom-404-error-depending-on-route) – Martin Tournoij Mar 07 '16 at 06:12
  • laravel version is different and these methods are not present in laravel 5.2 as suggested in this link, i tried this but failed, plz give any other link for solution @Carpetsmoker – Abid Ali Mar 07 '16 at 09:07

4 Answers4

17

use abort(404);

Some exceptions describe HTTP error codes from the server. For example, this may be a "page not found" error (404), an "unauthorized error" (401) or even a developer generated 500 error. In order to generate such a response from anywhere in your application, use the following:

abort(404);

If you invoke abort(404); anywhere in your route or controller it will throw HTTPNotFoundException which looks for a blade template to display in resources/views/errors/ directory with the filename same as the error code.

Example:

in your app/Http/routes.php

Route::get('/test', function(){
   return abort(404);
});

in your resources/views/errors/ directory create 404.blade.php, notice the name of the file corresponds with the abort(404);

Reference: https://laravel.com/docs/5.2/errors#http-exceptions

Jim M
  • 4,261
  • 1
  • 18
  • 15
3

Modify app/Exceptions/Handler.php and add some lines as below:-

public function render($request, Exception $e)
{
  if ($e instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException) 
  {
    return redirect('/');
  } 
 return parent::render($request, $e); }

Use your route name in place of / where you want to redirect.

Pawan Verma
  • 1,152
  • 14
  • 22
1

Create a view and set this code in app/Exceptions/Handler.php :

use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
.
.
.
public function render($request, Exception $exception)
{
    if($exception instanceof NotFoundHttpException)
    {
        return response()->view('missing', [], 404);
    }
    return parent::render($request, $e);
}
Arash Hatami
  • 5,297
  • 5
  • 39
  • 59
0

in the /resources/views/errors directory there is a 404 page

edit this page and you can make it look like you want

andylondon
  • 176
  • 6