Laravel 4 manages 404 errors

I am new to Laravel and I am trying to catch any requests that do not match existing routes. I am currently getting ...

Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException

I created the App::missing() handler in app/start/global.php , as described on the documentation page , but that does not help. I also tried to create an App::error(function(NotFoundHttpException $exception, $code) handler, but that didn't help either.

Any help would be appreciated.

+8
laravel laravel-4
source share
5 answers

This is probably due to the fact that debugging is enabled in the app/config/app.php . Try turning this value to false and see if your user-defined handler exists and what "processes" it.

In addition, your error handler should return a response to short-circuit the request and respond to it using the error handler. Does the class / response value return from your App::error() handler? (Please show us this code).

Here's an article about Laravel 4 Error Handling that does not describe how Laravel is used by the App::missing() and App::error() handlers when errors occur.

Pay particular attention to the "meat of the Handler class" section - it describes how you need to return some value from the handler so that it does not throw an exception to the next handler (probably your Whoops error output, which is displayed when debug set to true in the app/config/app.php ).

+3
source share

You need the following: In 'app / start / global.php' add:

 App::missing(function($exception) { return Response::view('errorView', array(), 404); }); 

And, of course, in the views is created (in this case) errorView.blade.php
EDIT: This method handles all 404 Not Found errors.

+20
source share

Here's how to execute a 404 error when a ModelNotFoundException occurs

 App::error(function(Illuminate\Database\Eloquent\ModelNotFoundException $exception, $code) { return Response::view('errors.404', array(), 404); }); 
+3
source share

in

app -> config -> app.php

if you do

 'debug' => true, 

to that

 'debug' => false, 

You will get a user interface environment other than debug mode. Make sure you check it.

Thanks:)

+1
source share

I managed to do this using the following code. I enable 'debug' => true in my local configuration.

In app / start / global.php

 App::error(function(Symfony\Component\HttpKernel\Exception\NotFoundHttpException $exception) { Log::error($exception); $response = Response::make(View::make('response') ->with('data', null) ->with('meta', 'Error, incorrect path') ->with('pagination', null)); $response->setStatusCode(404); $response->header('content-type', 'application/json', true); return $response; }); 

If any interest, the content of my submission

 <?php echo json_encode(array( 'data' => $data, 'pagination' => $pagination, 'meta' => $meta), JSON_PRETTY_PRINT); ?> 

The main problem was to use Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException as an exception type, not just NotFoundHttpException.

Using NotFoundHttpException, a 500 error was thrown.

0
source share

All Articles