Recommend this page to a friend! |
Download .zip |
Info | Documentation | View files (20) | Download .zip | Reputation | Support forum | Blog | Links |
Last Updated | Ratings | Unique User Downloads | Download Rankings | |||||
2020-04-29 (4 months ago) | Not yet rated by the users | Total: 61 This week: 1 | All time: 9,784 This week: 274 |
Version | License | PHP version | Categories | |||
mezon-router 1.0 | MIT/X Consortium ... | 7.2 | HTTP, PHP 7 |
Description | Author | |
This package can route HTTP requests mapping URLs into classes. |
Mezon provides simple routing class for your needs.
Just print in console
composer require mezon/router
And that's all )
The mezon/router is more than 25 times faster then klein/klein router (in some cases).
Router allows you to map URLs on your php code and call when ever it needs to be calld.
Router supports simple routes like in the example above - example.com/contacts/
Each Application object implicity creates routes for it's 'action[action-name]' methods, where 'action-name' will be stored as a route. Here is small (as usual)) ) example:
class MySite
{
/
* Main page.
*/
public function actionIndex()
{
return 'This is the main page of our simple site';
}
/
* Contacts page.
*/
public function actionContacts()
{
return 'This is the "Contacts" page';
}
/
* Some custom action handler.
*/
public function someOtherPage()
{
return 'Some other page of our site';
}
public static function someStaticMethod()
{
return 'Result of static method';
}
}
And this code
$router = new \Mezon\Router\Router();
$router->fetchActions( $mySite = new MySite() );
will create router object and loads information about it's actions and create routes. Strictly it will create two routes, because the class MySite has only two methods wich start wth 'action[Suffix]'. Method 'someOtherPage' will not be converted into route automatically.
But we can still use this method as a route handler:
$router->addRoute( '/some-any-other-route/' , [ $mySite , 'someOtherPage' ] );
And you also can use stati methods:
$router->addRoute( '/static-route/' , [ 'MySite' , 'someStaticMethod' ] );
// or in this way
$router->addRoute( '/static-route/' , 'MySite::someStaticMethod' );
We just need to create it explicitly.
We can also use simple functions for route creation:
function sitemap()
{
return( 'Some fake sitemap' );
}
$router->addRoute( '/sitemap/' , 'sitemap' );
You can specify one processor for all routes like this:
$router->addRoute( '/*/' , function(){} );
Note that routing search will stops if the '*' handler will be found. For example:
$router->addRoute( '/*/' , function(){} );
$router->addRoute( '/index/' , function(){} );
In this example route /index/ will never be reached. All request will be passed to the '*' handler. But in this example:
$router->addRoute( '/contacts/' , function(){} );
$router->addRoute( '/*/' , function(){} );
$router->addRoute( '/index/' , function(){} );
route /contacts/ will be processed by it's own handler, and all other routes (even /index/) will be processed by the '*' handler.
And now a little bit more complex routes:
$router->addRoute( '/catalogue/[i:cat_id]/' , function( $route , $variables ){} );
$router->addRoute( '/catalogue/[a:cat_name]/' , function( $route , $variables ){} );
Here:
i - any integer number a - any [a-z0-9A-Z_\/\-\.\@]+ string il - comma separated list of integer ids s - any string
All this variables are passed as second function parameter wich is named in the example above - $Variales. All variables are passed as an associative array.
You can bind handlers to different request types as shown bellow:
$router->addRoute( '/contacts/' , function(){} , 'POST' ); // this handler will be called for POST requests
$router->addRoute( '/contacts/' , function(){} , 'GET' ); // this handler will be called for GET requests
$router->addRoute( '/contacts/' , function(){} , 'PUT' ); // this handler will be called for PUT requests
$router->addRoute( '/contacts/' , function(){} , 'DELETE' ); // this handler will be called for DELETE requests
Files |
File | Role | Description | ||
---|---|---|---|---|
doc (6 files) | ||||
Tests (5 files) | ||||
.travis.yml | Data | Auxiliary data | ||
composer.json | Data | Auxiliary data | ||
composer.lock | Data | Auxiliary data | ||
phpunit.xml | Data | Auxiliary data | ||
README.md | Doc. | Documentation | ||
Router.php | Class | Class source | ||
RoutesSet.php | Class | Class source | ||
UrlParser.php | Class | Class source | ||
Utils.php | Class | Class source |
Files | / | doc |
File | Role | Description |
---|---|---|
graph-klein.png | Data | Auxiliary data |
graph-symfony.png | Data | Auxiliary data |
router-symfony.md | Data | Auxiliary data |
router.md | Data | Auxiliary data |
table-klein.png | Icon | Icon image |
table-symfony.png | Data | Auxiliary data |
Files | / | Tests |
File | Role | Description |
---|---|---|
DynamicRoutesInvalidCasesUnitTest.php | Class | Class source |
DynamicRoutesUnitTest.php | Class | Class source |
RouterUnitTest.php | Class | Class source |
RouterUtilsUnitTest.php | Class | Class source |
StaticRoutesUnitTest.php | Class | Class source |
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.