Search code examples
laravelparametersroutesmiddleware

laravel middleware with parameter in route executed twice


I have this very simple middleware

    namespace App\Http\Middleware;

    use Closure;
    use Illuminate\Http\Request;
    use Illuminate\Support\Facades\Log;

    class CacheResponseMinify
    {
        /**
         * Handle an incoming request.
         *
         * @param  \Illuminate\Http\Request  $request
         * @param  \Closure  $next
         * @return mixed
         */
        public function handle(Request $request, Closure $next)
        {
            Log::debug('***********************************************************');
            Log::debug($request);
            return $next($request);
        }
    }

This middleware is associated with a route and a parameter :

    Route::get('/route/{page}', [App\Http\Controllers\MyController::class, 'page'])->middleware([ 'cacheResponseMinify']);

I enter this URL in browser : http://localhost:81/route/my_page

In the log, the first execution of hte middleware shows an empty array :

    [2022-03-01 14:38:48] local.DEBUG: array (
    )  

The second execution shows an array with datas from the loaded URL

    [2022-03-01 14:38:51] local.DEBUG: array (
      'action_name' => 'Title_of_the_page',
      'idsite' => '0',
      'rec' => '1',
      'r' => '588131',
      'h' => '14',
      'm' => '38',
      's' => '50',
      'url' => 'http://localhost:81/route/my_page',
      '_id' => '746a68055b9493f2',
      '_idts' => '1646141931',
      '_idvc' => '1',
      '_idn' => '1',
      '_refts' => '0',
      '_viewts' => '1646141931',
      'send_image' => '1',
      'pdf' => '1',
      'qt' => '0',
      'realp' => '0',
      'wma' => '0',
      'dir' => '0',
      'fla' => '0',
      'java' => '0',
      'gears' => '0',
      'ag' => '0',
      'cookie' => '1',
      'res' => '1920x1080',
      'gt_ms' => '404',
      'pv_id' => 'iGGVy6',
    )  

If the route is absolute, wihout parameter

    Route::get('/route/my_page', [App\Http\Controllers\MyController::class, 'page'])->middleware([ 'cacheResponseMinify']);

the middleware is executed only once.

Have you some idea why with a route and parameter, the middleware is executed twice ?


Solution

  • I ended up finding the source of the problem.

    For each page, i set up statistical monitoring with Matomo. There is a script matomo.js which triggers the call to matomo.php?list_of_parameters.

    By consulting http://localhost:81/route/my_page, the matomo script calls http://localhost:81/route/matomo.php?list_of_parameters

    In the route, there are: Route::get('/route/{page}'

    http://localhost:81/route/my_page and http://localhost:81/route/matomo.php?list_of_parameters both pass by this route.

    In the controller, my_page corresponds to a specific view, OK, caching is correct.

    On the other hand, matomo.php?list_of_parameters goes on the default view which is the home page of the directory

    Laravel and caching therefore behave completely normally :-)

    The error was elsewhere.

    The matomo.js file uses parameters defined in matomo_appli.js.

    In the files matomo_appli.js is defined URL_MATOMO. For local environnment and dev environnment, it was an empty string.

    That's why the stats url called was http://localhost:81/route/matomo.php?list_of_parameters

    I modified to put URL_MATOMO with a link pointing to http://localhost:81/matomo/

    Now, the matomo stat links are of the form http://localhost:81/matomo/matomo.php?list_of_parameters

    And so, no more going through a Laravel route or unplanned caching.