ngabor84 / lumen-api-query-parser
A query parser for REST-APIs based on laravel's lumen framework
Installs: 1 471
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 1
Forks: 5
Open Issues: 4
Requires
- php: ^7.3
- laravel/lumen-framework: ^6.2
Requires (Dev)
- mockery/mockery: ^1.3
- nunomaduro/phpinsights: ^1.11
- phpunit/phpunit: ^8.4
This package is auto-updated.
Last update: 2024-12-23 03:11:22 UTC
README
Description
This is a simple request query parameter parser for REST-APIs based on Laravel's Lumen framework.
Requirements
- PHP >=7.1
- Lumen framework >= 5.4
- Mockery >= 0.9 (dev)
- PHPUnit >= 6.1 (dev)
- PHP CodeSniffer >= 3.0.0 RC4 (dev)
Installation
- Add ngabor84/lumen-api-query-parser to your composer.json and make composer update, or composer require ngabor84/lumen-api-query-parser ~1.0
- Setup the service provider:
in bootstrap/app.php add the following line:
$app->register(LumenApiQueryParser\Provider\RequestQueryParserProvider::class);
Usage
// app/API/V1/Models/UserController.php namespace App\Api\V1\Http\Controllers; use App\Api\V1\Models\User; use App\Api\V1\Transformers\UserTransformer; use LumenApiQueryParser\ResourceQueryParserTrait; use LumenApiQueryParser\BuilderParamsApplierTrait; class UserController extends Controller { use ResourceQueryParserTrait; use BuilderParamsApplierTrait; public function index(Request $request) { $params = $this->parseQueryParams($request); $query = User::query(); $userPaginator = $this->applyParams($query, $params); $this->response->paginator($userPaginator, new UserTransformer, ['key' => 'users']); } }
Query syntax
Eager loading
Q: /users?connection[]=profile
R: will return the collection of the users with their profiles included
Filtering
Q: /users?filter[]=name:ct:admin
R: will return the collection of the users whose names contains the admin string
Available filter options
Sorting
Q: /users?sort[]=name:ASC
R: will return the collection of the users sort by their names ascending
Pagination
Q: /users?limit=10&page=3
R: will return a part of the collection of the users (from the 21st to 30th)