Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Validation - Rule to disallow request parameters

In my Laravel 5.8 app I have many API routes which return paginated results. If I make a request to my API appending the following query string I can disable pagination.

http://api.test/users/?no_paginate=1

My question is... how can I disable no_paginate from being used on certain routes? I'd preferbly want some validation to go in the request class but I can't find anything in the docs for that.

like image 896
itsliamoco Avatar asked May 23 '19 05:05

itsliamoco


1 Answers

You can do this using a Global Middleware.

  1. Create a DisableNoPaginate Middleware:

    php artisan make:middleware DisableNoPaginate
    
  2. Then define what the middleware should do (DisableNoPaginate.php):

    <?php
    namespace App\Http\Middleware;
    
    use Closure;
    
    class DisableNoPaginate
    {
        public function handle($request, Closure $next)
        {
            //remove no_paginate param from request object
            unset($request['no_paginate']);
    
            return $next($request);
        }
    }
    
  3. Arrange for the middleware to run on all routes (routes.php):

    $app->middleware([
        App\Http\Middleware\DisableNoPaginate::class
    ]);
    

Now the no_paginate query param should be stripped from all your incoming requests.

like image 51
Sapnesh Naik Avatar answered Sep 22 '22 16:09

Sapnesh Naik