Laravel - input request handling for global pagination

- 1 answer

Ad

So I am creating an api in Laravel and a majority of the calls being made to it are requests for sortable / filterable / paginated data that will be displayed in data tables. I would like to create "something" that checks every request (sounds like middleware maybe??) and rips out the appropriate sorting / filtering / pagination data and stores it ...."somewhere".

My question is what would be that "something" (middleware? trait?) and in implementing it where would be a good place to store said information. Obviously not in a global variable....if it was a trait maybe I would store it in class properties? I am grasping at straws here and just need a point in the right direction.

Ad

Answer

Ad

If you're using Laravel 5 you can easily setup whole thing in the middleware directory located in app/http/Middleware. You create new class there, register it in app/Http/Kernel.php file in $middleware property and use your newly running middleware however you like.

Super simple example:

<?php
namespace App\Http\Middleware;

use Closure;

class StoreInfo
{
    public function handle($request, Closure $next)
    {
        // Tear your $request apart here and store however you need.
    }

}

As usually - it is advised to do composer dump-autoload after creating new file.

Ad
source: stackoverflow.com
Ad