Log Route calls in Laravel using Middleware

 Reading time ~1 minute

Heads up: this article is over a year old. Some information might be out of date, as I don't always update older articles.

Introduction

Sometimes when developing a new site or application in Laravel you need to log exactly the route calls in order to track bugs and errors. This operation is straightforward.

First, let’s create a new Middleware

php artisan make:middleware LogRouteMiddleware

Then edit the handle function like the following:

<?php

public function handle($request, Closure $next)
{
    Log::info($request->fullUrl());

    return $next($request);
}

The fullUrl method logs the full URL including all url and query parameters. You can decide also to log other information, for example:

  • ip() returns the client IP address
  • isJson() determine if the request is sending JSON

Pro-tip: The API documentation for the Request class is available at http://laravel.com/api/5.1/Illuminate/Http/Request.html

Finally in app/Http/Kernel.php you can include your Middleware to the list

<?php

protected $middleware = [
    ...
    'App\Http\Middleware\LogRouteMiddleware',
    ...
];
comments powered by Disqus

Work on a Laravel Live Site while in Maintenance Mode

Introduction

Since Laravel 4 there’s a handy feature that allows you to put your site in maintenance mode, providing a …