Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel-4: How to find route for NotFoundHttpException?

Tags:

php

laravel-4

I found a NotFoundHttpException in the logs. It looks like this:

[2013-11-26 13:49:20] log.ERROR: exception 'Symfony\Component\HttpKernel\Exception\NotFoundHttpException' in /var/www/myproject/vendor/laravel/framework/src/Illuminate/Routing/Router.php:1429
Stack trace:
#0 /var/www/myproject/vendor/laravel/framework/src/Illuminate/Routing/Router.php(1050): Illuminate\Routing\Router->handleRoutingException(Object(Symfony\Component\Routing\Exception\ResourceNotFoundException))
#1 /var/www/myproject/vendor/laravel/framework/src/Illuminate/Routing/Router.php(1014): Illuminate\Routing\Router->findRoute(Object(Illuminate\Http\Request))
#2 /var/www/myproject/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(530): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#3 /var/www/myproject/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(506): Illuminate\Foundation\Application->dispatch(Object(Illuminate\Http\Request))
#4 /var/www/myproject/public/index.php(49): Illuminate\Foundation\Application->run()
#5 {main} [] []

This tells you nothing and is just a waste of disk space.

How can I find the URI that is causing an NotFoundHttpException?

like image 993
PiTheNumber Avatar asked Nov 26 '13 14:11

PiTheNumber


2 Answers

in app/start/global.php extend App::error():

App::error(function(Exception $exception, $code)
{
    if ($exception instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException)
    {
        Log::error('NotFoundHttpException Route: ' . Request::url() );
    }

    Log::error($exception);
});

Now you get an additinal log entry with the URL:

 [2013-11-26 14:20:07] log.ERROR: NotFoundHttpException Route: http://myproject.net/asdfgsdfghsdfg [] []
like image 98
PiTheNumber Avatar answered Oct 31 '22 14:10

PiTheNumber


You can filter 404 (NotFoundHttpException) error form your log file.
File location : app/start/global.php

App::error(function(Exception $exception, $errorCode)
{
    $requestUrl = Request::fullUrl(); 
    $userAgent = Request::header('user-agent');

    if($errorCode != 404){
        Log::error('Exception', array(
            'errorCode' => $errorCode,
            'requestUrl' => $requestUrl,
            'userAgent' => $userAgent,
            'context' => $exception,
        ));     
    }

    return Response::view('error-page-path.error-404', array(), 404);
    // Here "error-404" is a blade view file in "error-page-path" directory
});
like image 23
Shojib Flamon Avatar answered Oct 31 '22 13:10

Shojib Flamon