Recommend this page to a friend! |
Download |
Info | Example | Files | Install with Composer | Download | Reputation | Support forum | Blog | Links |
Ratings | Unique User Downloads | Download Rankings | ||||
Not yet rated by the users | Total: 55 | All time: 10,593 This week: 673 |
Version | License | PHP version | Categories | |||
zestrouter 1.0.0 | MIT/X Consortium ... | 5 | HTTP, PHP 5, Configuration, Design Pa... |
Description | Author | |||||||||||||||||||||||
This package can configure URL routes and dispatch HTTP requests. |
|
<?php |
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']);
Run the command in terminal/cmd
# Getting started
## Requirements
- PHP 7 or newest
- Composer
## Rewrite all requests
### Apache (.htaccess)
RewriteEngine On
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 (7) |
File | Role | Description | ||
---|---|---|---|---|
example (2 files, 1 directory) | ||||
src (1 file) | ||||
composer.json | Data | Composer package configuration file | ||
LICENSE | Lic. | License text | ||
readme.md | Doc. | Example script |
Files (7) | / | example |
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 |
Version Control | Unique User Downloads | Download Rankings | |||||||||||||||
100% |
|
|
Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.