PHP code example of leandroibarra / lumen-request-parser

1. Go to this page and download the library: Download leandroibarra/lumen-request-parser library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

leandroibarra / lumen-request-parser example snippets


    $app->register(LumenRequestParser\Providers\RequestParserProvider::class);
    

    // app/Http/Controllers/UserController.php
    namespace App\Http\Controllers;
    
    use App\Models\User;

    use LumenRequestParser\Traits\RequestParserTrait;
    use LumenRequestParser\Traits\RequestBuilderApplierTrait;
    
    class UserController extends Controller
    {
        use RequestParserTrait;
        use RequestBuilderApplierTrait;
                
        public function index(Request $request)
        {
            /**
             * I suggest to make a first level request validation. For example:
             * $this->validate($request, [
             *      'page' => 'nullable|integer|min:1',
             *      'limit' => 'nullable|integer|min:1|max:100',
             *      'filter' => [
             *          'nullable',
             *          'regex:/^([a-zA-Z]+:(ct|nct|sw|ew|eq|ne|gt|ge|lt|le|in|nin)+:[^,]+,)*([a-zA-Z]+:(ct|nct|sw|ew|eq|ne|gt|ge|lt|le|in|nin)+:[^,]+)$/i',
             *      ],
             *      'sort' => [
             *          'nullable',
             *          'regex:/^([-+]?[a-zA-Z]+(,[-+]?[a-zA-Z]+)*)?$/i'
             *      ]
             * ]);
             */

            $params = $this->parseQueryParams($request);
            /**
             * Or even you can set sorting or page size (limit) options. For example:
             * $options = ['sort' => 'email', 'limit' => 50];
             * $params = $this->parseQueryParams($request, $options);
             */

            $query = User::query();
            $userPaginator = $this->applyParams($query, $params);

            return response()->json($userPaginator);
        }
    }