1. Go to this page and download the library: Download mekramy/support 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/ */
use MEkramy\Support\Paginator;
use Illuminate\Routing\Controller;
class UserController extends Controller
{
public function index(Paginator $paginator)
{
// Set valid sorts
// if invalid sort passed by request first sort used
$paginator->setSorts(['id', 'username', 'firstname', 'lastname']);
// or
$paginator->sorts = ['id', 'username', 'firstname', 'lastname'];
// Set valid limits, default is [10, 25, 50, 100]
// if invalid limit passed by request first limit used
$paginator->setLimits([25, 50, 100]);
// or
$paginator->limits = [25, 50, 100];
// Parse page, limit, sort, order, search and tags from request
$paginator->parse();
// Set default meta (page, limit, sort, order, search and tags)
$paginator->setPage((int) $request->page);
// or
$paginator->limit = 10;
// Get default meta
$sort = $paginator->sort;
// or
$order = $paginator->getOrder();
// Set extra meta
$paginator->addMeta('author', 'm ekramy');
// or simply add Meta to end of tag name and set by property
$paginator->authorMeta = 'm ekramy';
// Get extra meta
$author = $paginator->getMeta('author');
// or simply add Meta to end of tag name and get by property
$author = $paginator->authorMeta;
// Set query
$paginator->setQuery(User::where('is_banned', false));
// you can access query by query property or getter methods
$paginator->query->where('firstname', 'like', "%{$paginator->search}%")
// Tags: tags are list of filters come in base64 encoded string and parsed as array
// Check if tag passed
// in this example accesses is array of access list ex: ["admin", "support"]
if($paginator->hasTag('accesses')){
$paginator->query->whereIn('access', $paginator->getTag('access'))
}
// Access tag
$bannedOnly = $paginator->getTag('banned');
// or simply add Tag to end of tag name and get by property
$bannedOnly = $paginator->bannedTag;
// Add Tag to final response and send to user
$paginator->addTag('gender', 'male');
// or simply add Tag to end of tag name and set by property
$paginator->genderTag = 'male';
// Get paginated result
$arrayOfResult = $paginator->toArray();
$jsonOfResult = $paginator->toJson();
// or you can simply return paginator as result (return json)
return $paginator;
/* returned response
* {
* "meta": {
* "page": 1,
* "limit": 10,
* "sort": "id",
* "order": "asc",
* "search": "",
* "author": "m ekramy",
* "tags": "eyJnZW5kZXIiOiJtYWxlIiwiYWNjZXNzZXMiOlsiYWRtaW4iLCJzdXBwb3J0Il19"
* },
* "pagination": {
* "current_page": 1,
* "from": 1,
* "last_page": 11,
* "per_page": 10,
* "to": 10,
* "total": 103
* },
* "data": {...}
* }
}
}
use MEkramy\Support\Paginator;
use Illuminate\Routing\Controller;
class LoginController extends Controller
{
public function index(RateLimiter $limiter)
{
// Set rate limiter parameters
// $limiter->init(string $key, int $maxAttempts: 3, int $decaySeconds: 300);
$limiter->init('key', 5, 600);
// or you can set parameter separately using setter or property
$limiter->setKey('key');
$limiter->key = 'key'
$limiter->setMaxAttempts(5);
$limiter->maxAttempts = 5
$limiter->setDecaySeconds(600);
$limiter->decaySeconds = 600;
// Check if limiter must look
if($limiter->mustLock()){
return response()->error('too many attempts', 419);
// or return number of seconds until unlock
return response()->error($limiter->availableIn(), 419);
}
// increase number of try. also return current number of attempts.
$totalAttempts = $limiter->addAttempts();
// reset rate limiter
$limiter->reset;
// get number of retries left
$retriesLeft = $limiter->retriesLeft();
// get number of second until available to unlock
$av = $limiter->availableIn();
// Lock rate limiter in special case
if($someThingHappen) {
$limiter->lock();
}
}
}