PHP Classes

Zest PHP Router Library: Configure URL routes and dispatch HTTP requests

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 55 All time: 10,593 This week: 673Up
Version License PHP version Categories
zestrouter 1.0.0MIT/X Consortium ...5HTTP, PHP 5, Configuration, Design Pa...
Description 

Author

This package can configure URL routes and dispatch HTTP requests.

It can add the configuration of handling requests that match certain URL patterns and dispatches them to given closure functions.

The package can also route requests to given controller classes by calling their functions with names that match the configured action.

Picture of Muhammad Umer Farooq
Name: Muhammad Umer Farooq is available for providing paid consulting. Contact Muhammad Umer Farooq .
Classes: 52 packages by
Country: Pakistan Pakistan
Age: 23
All time rank: 84310 in Pakistan Pakistan
Week rank: 206 Up5 in Pakistan Pakistan Up
Innovation award
Innovation award
Nominee: 6x

Example

<?php

use Lablnet\ZestRouter;

require_once
"../vendor/autoload.php";

$router = new ZestRouter;

//Namespaces uses for loading controllers olny
//$router->setDefaultNamespace("App\Controllers\\");

$router->get('', function () {
    echo
'Example route using closure';
});
/*
//OR
$router->add('', function () {
    echo 'Example route using closure';
},'GET');
*/
$router->get('test','Home@index');
/*
 //OR
 $router->get('test',['controller' => 'Home', 'action' => 'index']);
 //OR
  $router->add('test',['controller' => 'Home', 'action' => 'index'],'GET');

*/


//Dispatch/Process the request automatically for mannually dispatch request take a look at Process Request section
$router->dispatch($_SERVER['QUERY_STRING']);


Details

ZestRouter

ZestRouter is a small but powerful routing class for php

<?php 

use Lablnet\ZestRouter;

require_once "../vendor/autoload.php";

$router = new ZestRouter;

//Namespaces uses for loading controllers olny
//$router->setDefaultNamespace("App\Controllers\\");

$router->get('', function () {
    echo 'Example route using closure';
});
/*
//OR
$router->add('', function () {
    echo 'Example route using closure';
},'GET');
*/
$router->get('test','Home@index');
/*
 //OR
 $router->get('test',['controller' => 'Home', 'action' => 'index']);
 //OR
  $router->add('test',['controller' => 'Home', 'action' => 'index'],'GET');

*/
//Dispatch/Process the request automatically for mannually dispatch request take a look at Process Request section
$router->dispatch($_SERVER['QUERY_STRING']);

Features

  • Can be used with all HTTP Methods
  • Flexible regular expression routing
  • Custom regexes
  • Router with controllers by namespaces
  • Routers using closure

install

Run the command in terminal/cmd


# Getting started

## Requirements

- PHP 7 or newest
- Composer

## Rewrite all requests

### Apache (.htaccess)

Remove the question mark from the request but maintain the query string

RewriteEngine On

Uncomment the following line if your public folder isn't the web server's root

RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-l RewriteRule ^(.*)$ index.php?$1 [L,QSA]


### Nginx (nginx.conf)

location / {

if (!-f $request_filename){
    set $rule_0 1$rule_0;
}   
if (!-d $request_filename){
    set $rule_0 2$rule_0;
}
if ($rule_0 = "21"){
    rewrite ^/(.*)$ /index.php?$1 last;
}   

}


## Adding Routes

By now, you should have rewrite all requests

### simple (default way)
there are many to two ways to add the routes

#### Using add method

$router->add('', function () {
    echo "Welcome";
},'GET'); 

The `add()` method accepts the following parameters.

`$route` (string)
This is the route pattern to match against. This can be a plain string, a custom regex.

`$params` (array|string|Closure)
This is paramter for controllers and controller method or closure
(array) => ['controller' => 'Home', 'action' => 'index'] 
(string) => "Home@index" 
(closure) => function () { echo "Welcome"; }

`$method` (string)
This is a pipe-delimited string of the accepted HTTP requests methods.

Example: GET|POST|PATCH|PUT|DELETE

#### Using rests method

$router->get('', function () {
    echo "Welcome";
}); 

there are 5 request methods supports
`get(),post(),put(),patch(),delete()``

These methods accepts the following parameters.

`$route` (string)
This is the route pattern to match against. This can be a plain string, a custom regex.

`$params` (array|string|Closure)
This is paramter for controllers and controller method or closure
(array) => ['controller' => 'Home', 'action' => 'index'] 
(string) => "Home@index" 
(closure) => function () { echo "Welcome"; }

## Example adding the routes

// add homepage using callable $router->get( '/home', function() {

require __DIR__ . '/views/home.php';

});

// add users details page using controller@action string $router->get( 'users/{id:[0-9]+}', 'UserController@showDetails' );

For quickly adding multiple routes, you can use the addRoutes method. This method accepts an array or any kind of traversable.

$router->addRoutes(array( array('users/{username:\w+}', 'users@view', 'get'), array('users/{id:\d+}', 'users@update', 'PATCH'), array('users/{id:\d+}', 'users@delete', 'DELETE') ));



# Matching Requests

To match the current request, just call the `customDispatch()` method without any parameters.

$match = $router->customDispatch();


If a match was found, the `customDispatch()` method will return an associative array

# Processing Requests

ZestRouter process requests for you but so you are free to use the method you prefer. To help you get started, here's a simplified example using closures.

//Add the routes $router->get('', function () {

echo 'Example route using closure';

}); $router->get('user/{id:\d+}', function ($args) {

echo 'Example route using closure with params id: ' .$args['id'];

});

// match current request url $match = $router->customDispatch(); if ($match && is_callable( $match['callable'] )) {

call_user_func( $match['callable'], $match ); 

} else {

// no route was matched
echo '404 Not Found';

}


  Files folder image Files (7)  
File Role Description
Files folder imageexample (2 files, 1 directory)
Files folder imagesrc (1 file)
Accessible without login Plain text file composer.json Data Composer package configuration file
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file readme.md Doc. Example script

  Files folder image Files (7)  /  example  
File Role Description
Files folder imageApp (1 directory)
  Accessible without login Plain text file .htaccess Data Apache access configuration
  Accessible without login Plain text file index.php Example Example script

  Files folder image Files (7)  /  example  /  App  
File Role Description
Files folder imageControllers (1 file)

  Files folder image Files (7)  /  example  /  App  /  Controllers  
File Role Description
  Plain text file Home.php Class Class source

  Files folder image Files (7)  /  src  
File Role Description
  Plain text file ZestRouter.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:55
This week:0
All time:10,593
This week:673Up