PHPBook Router is a lightweight and fast router PHP library to create monolithic apps and apis.
Composer Install
composer require phpbook/router
Examples
Declare Configurations
<?php/********************************************* * Declare Configurations* * ******************************************///Output for structured data response. JSON or XML. //Not used in buffer dispatch.//Default "json"\PHPBook\Router\Configuration\Output::setType('json');//Exception template output format, where @ contains the string message//Default ['type' => 'exception', 'message' => '@']//Dispatch using the output Type\PHPBook\Router\Configuration\Output::setException(['type'=>'exception', 'message'=>'@']);//Content template output format, where @ contains the response data structure//Default ['type' => 'success', 'content' => '@']//Dispatch using the output Type\PHPBook\Router\Configuration\Output::setContent(['type'=>'success', 'content'=>'@']); ?>
Declare Application Elements
<?php/********************************************* * Declare Application Elements* * ******************************************/classAuthenticationElementextends\PHPBook\Router\Element {publicfunction__construct() {$this->setParameter('My-Key', new\PHPBook\Router\Parameter\Value('header key auth'));$this->setParameter('User-Agent', new\PHPBook\Router\Parameter\Value('header key auth'));$this->setParameter('Cache-Control', new\PHPBook\Router\Parameter\Value('header key auth')); }}classCustomerQueryElementextends\PHPBook\Router\Element {publicfunction__construct() {$this->setParameter('ageStarts', new\PHPBook\Router\Parameter\Value('age starts with')); }}classFoodElementextends\PHPBook\Router\Element {publicfunction__construct() {$this->setParameter('id', new\PHPBook\Router\Parameter\Value('food id description'));$this->setParameter('name', new\PHPBook\Router\Parameter\Value('food name description')); }}classFriendElementextends\PHPBook\Router\Element {publicfunction__construct() {$this->setParameter('id', new\PHPBook\Router\Parameter\Value('friend id description'));$this->setParameter('name', new\PHPBook\Router\Parameter\Value('friend name description'));$this->setParameter('bestFood', new\PHPBook\Router\Parameter\One('FoodElement', 'best food'));$this->setParameter('foods', new\PHPBook\Router\Parameter\Many('FoodElement', 'all foods')); }}classCustomerElementextends\PHPBook\Router\Element {publicfunction__construct() {$this->setParameter('id', new\PHPBook\Router\Parameter\Value('customer id description'));$this->setParameter('name', new\PHPBook\Router\Parameter\Value('customer name description'));$this->setParameter('age', new\PHPBook\Router\Parameter\Value('customer age description'));$this->setParameter('friends', new\PHPBook\Router\Parameter\Many('FriendElement', 'all friends'));$this->setParameter('bestFriend', new\PHPBook\Router\Parameter\One('FriendElement', 'best friend')); }}/********************************************** * Get Elements Parameters* * *******************************************///Get one parameter$customer=newCustomerElement;$parameter=$customer->getParameter('name');$parameter->getDescription();//Get all parameters$customer=newCustomerElement;$parameters=$customer->getParameters();foreach($parametersas$name=>$parameter) {$description=$parameter->getDescription();};/********************************************** * Work With Element Query Manually* * *******************************************/$element='CustomerElement';$description='Customer Description';$rules= ['only'=> ['name']];$user=new\StdClass;$user->name='Jhon';//Get query with ony element$query=new\PHPBook\Router\Query(new\PHPBook\Router\Parameter\One($element, $description), $rules);$query->schema(); //get element schema recursively following the rules$query->intercept($user); //get element with data according to the schema recursively following the rules//Get query with many of element$query=new\PHPBook\Router\Query(new\PHPBook\Router\Parameter\Many($element, $description), $rules);$query->schema(); //get element schema recursively following the rules$query->intercept([$user]); //get element with data according to the schema recursively following the rules?>
Declare Application Controllers
<?php/********************************************** * Declare Application Controllers* * *******************************************/classCustomerController {publicfunctionpost($inputs, $output) {//inside the $inputs primitive values, the whitespace are stripped from the beginning and end$customer=newstdClass();$customer->id=10;$customer->name=$inputs->body->name;$customer->age=$inputs->body->age;//$customer->save();//inside the $output primitive values, the whitespace are stripped from the beginning and endreturn$output->intercept($customer); }publicfunctionput($inputs, $output) {//inside the $inputs primitive values, the whitespace are stripped from the beginning and end//get by $inputs->uri->id to edit;$customer=newstdClass();$customer->id=$inputs->uri->id;$customer->name=$inputs->body->name;$customer->age=$inputs->body->age;//inside the $output primitive values, the whitespace are stripped from the beginning and endreturn$output->intercept($customer); }publicfunctionget($inputs, $output) {//inside the $inputs primitive values, the whitespace are stripped from the beginning and end//get by $inputs->uri->id;$customer=newstdClass();$customer->name='Jhon';$customer->age=10;//inside the $output primitive values, the whitespace are stripped from the beginning and endreturn$output->intercept($customer); }publicfunctionquery($inputs, $output) {//inside the $inputs primitive values, the whitespace are stripped from the beginning and end$customers= [];$jhon=newstdClass();$jhon->id=25;$jhon->name='Jhon';$jhon->age=10;foreach([$jhon, $paul] as$customer) {if ($inputs->query->ageStarts<=$customer->age) {$customers[] =$customer; }; };//inside the $output primitive values, the whitespace are stripped from the beginning and endreturn$output->intercept($customers); }publicfunctionphoto($inputs, $output) {//inside the $inputs primitive values, the whitespace are stripped from the beginning and end//get by $inputs->uri->id;//the $inputs->uri->alias is a practice that you can use just to control cache request;//you do not need use $inputs->uri->alias to get the file//$inputs->uri->alias can be the current file name or any other name.$customer=newstdClass();$customer->photo='@data-buffer-here';$buffer=$customer->photo;return$buffer; }publicfunctiondelete($inputs, $output) {//inside the $inputs primitive values, the whitespace are stripped from the beginning and end//get $inputs->header->{'My-Key'} to authentication;//get by $inputs->uri->id to delete;returnNull; }}?>
Register Application Request Categories
<?php/**************************************************** * Register Application Request Categories* * *************************************************/\PHPBook\Router\Request::setCategory((new\PHPBook\Router\Category)->setCode('customerCategory')->setName('Customers Resources'));/**************************************************** * Get All Application Request Categories* * *************************************************/$categories=\PHPBook\Router\Request::getCategories();foreach($categoriesas$category) {$code=$category->getCode();$name=$category->getName();};?>
Register Application Request Resources
<?php/**************************************************** * Register Application Request Resources* * *************************************************/\PHPBook\Router\Request::setResource((new\PHPBook\Router\Resource())->setCategoryCode('customerCategory')->setUri('customer/post')->setNotes('Any important note')->setType('post')->setInputBody('\PHPBook\Router\Parameter\One', 'CustomerElement', ['except'=> ['id']])->setController('CustomerController', 'post')->setOutput('\PHPBook\Router\Parameter\One', 'CustomerElement', []));\PHPBook\Router\Request::setResource((new\PHPBook\Router\Resource)->setCategoryCode('customerCategory')->setUri('customer/put/:id')->setNotes('Any important note')->setType('put')->setInputUri('\PHPBook\Router\Parameter\One', 'CustomerElement', ['only'=> ['id']])->setInputBody('\PHPBook\Router\Parameter\One', 'CustomerElement', ['except'=> ['id']])->setController('CustomerController', 'put')->setOutput('\PHPBook\Router\Parameter\One', 'CustomerElement', []));\PHPBook\Router\Request::setResource((new\PHPBook\Router\Resource())->setCategoryCode('customerCategory')->setUri('customer/get/:id')->setNotes('Any important note')->setType('get')->setInputUri('\PHPBook\Router\Parameter\One', 'CustomerElement', ['only'=> ['id']])->setController('CustomerController', 'get')->setOutput('\PHPBook\Router\Parameter\One', 'CustomerElement', ['except'=> ['id', 'friends.id']]));\PHPBook\Router\Request::setResource((new\PHPBook\Router\Resource())->setCategoryCode('customerCategory')->setUri('customer/query')->setInputQuery('\PHPBook\Router\Parameter\One', 'CustomerQueryElement', [])->setNotes('Any important note')->setType('get')->setController('CustomerController', 'query')->setOutput('\PHPBook\Router\Parameter\Many', 'CustomerElement', []));\PHPBook\Router\Request::setResource((new\PHPBook\Router\Resource())->setCategoryCode('customerCategory')->setUri('customer/get/:id/photo/:alias')->setNotes('Any important note')->setType('get')->setInputUri('\PHPBook\Router\Parameter\One', 'CustomerElement', ['only'=> ['id']])->setController('CustomerController', 'photo')->setIsBufferOutput(true)->setCacheHours(72));\PHPBook\Router\Request::setResource((new\PHPBook\Router\Resource())->setCategoryCode('customerCategory')->setUri('customer/delete/:id')->setNotes('Any important note with authentication')->setType('delete')->setInputHeader('\PHPBook\Router\Parameter\One', 'AuthenticationElement', [])->setInputUri('\PHPBook\Router\Parameter\One', 'CustomerElement', ['only'=> ['id']])->setController('CustomerController', 'delete'));/**************************************************** * Get All Application Request Resources* * *************************************************/$resources=\PHPBook\Router\Request::getResources();foreach($resourcesas$resource) {$categoryCode=$resource->getCategoryCode();$uri=$resource->getUri();$notes=$resource->getNotes();$type=$resource->getType();list($inputHeaderType, $inputHeaderElement, $inputHeaderRules) =$resource->getInputHeader();list($inputUriType, $inputUriElement, $inputUriRules) =$resource->getInputUri();list($inputQueryType, $inputQueryElement, $inputQueryRules) =$resource->getInputQuery();list($inputBodyType, $inputBodyElement, $inputBodyRules) =$resource->getInputBody();list($controller, $method) =$resource->getController();list($outputType, $outputElement, $outputRules) =$resource->getOutput();$isBufferOutput=$resource->getIsBufferOutput();$cacheHours=$resource->getCacheHours();};?>
Router URI Get Path
You can get server router string url base or string url base with a uri resource
<?php/**************************************************** * Router URI Get Path* * *************************************************/$base=\PHPBook\Router\Url::get();$resource=\PHPBook\Router\Url::get('customer/get/2');?>
Start Application Router
<?php/**************************************************** * Start Application Router* * *************************************************///check the script is not running in consoleif (!\PHPBook\Router\Router::isConsole()) {\PHPBook\Router\Router::start();};?>
Application Apache htaccess File
This htaccess must be in the same directory where you call \PHPBook\Router\Router::start();
Do not forget to change index.php file name inside Apache htaccess if necessary
The Tidelift Subscription provides access to a continuously curated stream of human-researched and maintainer-verified data on open source packages and their licenses, releases, vulnerabilities, and development practices.