1. Go to this page and download the library: Download mdantas/express-request 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/ */
mdantas / express-request example snippets
public function bootstrap(): void
{
//... other codes.
$this->addPlugin('ExpressRequest');
}
//Model/Table/DomainsTable.php
class DomainsTable extends Table implements ExpressRepositoryInterface {
//...code...
public function getFilterable(): FiltersCollection
{
return new FiltersCollection([
new BooleanFilter('active'),
new SearchFilter('name', SearchFilter::START_STRATEGY),
new SearchDateFilter('created_at'),
new NumberFilter('price'),
new SearchInFilter('type')
]);
}
public function getSelectables(): array
{
return [
'name',
'created_at',
'price',
'type',
'active'
];
}
}
//Controller/DomainsController.php
public function index()
{
// The finder need to be a method what return a Cake\ORM\Query object.
return $this
->responseJson(
$this->ExpressRequest->search(
$this->request,
$this->Domains,
'findDomainsByCompany', // Finder model method optional
$companyId // Optional
)
);
}
public function anotherWay() {
// We can make our filtering by request expressions
$collection = $this->ExpressRequest->search(
$this->request,
$this->Domains
);
$query = $collection->getQuery();
// And can implement our needed things above user api expressions.
$query->where(['id' => 1]);
return $this->responseJson($collection);
}
public function alternative()
{
// Request and Model now is taked from controller.
// Need status code
// Return a ResponseInterface from psr with json data on body.
return this->ExpressRequest->getResponse(200);
}
// http://localhost:8765/domains?nested=users,comments //Get data with relationship
// localhost/domains?price=100 - Exact by 100
// localhost/domains?price=100..200 - Between 100 and 200
// localhost/domains?price[lt|gt|lte|gte]=100 - filter less, great, less than or great than.
new SearchFilter('name')
// localhost/domains?name=null - WHERE name IS NULL
// localhost/domains?name[is]=null - WHERE name IS NULL
// localhost/domains?name[not]=null - WHERE name IS NOT NULL
new SearchFilter('name') - Exact is default
// localhost/domains?name=marcos - Exact by marcos
new SearchFilter('name', SearchFilter::PARTIAL_STRATEGY)
// localhost/domains?name=marcos - add a %value% by like method.
new SearchFilter('name', SearchFilter::START_STRATEGY)
// localhost/domains?name=marcos - add a value% by like method.
new SearchFilter('name', SearchFilter::END_STRATEGY)
// localhost/domains?name=marcos - add a %value by like method.
// localhost/domains?name=marcos - name in('marcos')
// localhost/domains?name=marcos,github - name in('marcos', 'github')
// localhost/domains?name[not]=marcos,github - name not in('marcos', 'github')
// localhost/domains?created_at=2019 - by init of 2019 year.
// localhost/domains?created_at=2019-01 - by init of Jan/2019 year.
// localhost/domains?created_at=201903 - by init of Mar/2019 year.
// localhost/domains?created_at=2019-01-12 - by day 12 of Jan/2019 year.
// localhost/domains?created_at=20190315 - by day 15 of Mar/2019 year.
// localhost/domains?created_at[lte]=20190315 - search by operators: [lt, lte, gt, gte]
// localhost/domains?created_at[lte]=20190315&created_at[gte]=20190315 - search by operators: [lt, lte, gt, gte]
[
'pagination' => true,
'maxSize' => 100, // Max number of per page.
'size' => 20, // default numbers of items per page
'ssl' => true, // generate routes with ssl by default,
'cacheConfig' => 'default', // cache config
'cache' => true, // Enable or disable cache
'reserved' => [ // If you need to use one o more of this keywords, change to alias.
'size' => 'size',
'page' => 'page',
'props' => 'props',
'nested' => 'nested',
'sort' => 'sort'
]
];