PHP Classes

mezon PHP Router Library: Route HTTP requests mapping URLs into classes

Recommend this page to a friend!
  Info   View files Documentation   View files View files (20)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2020-04-29 (1 month ago) RSS 2.0 feedNot yet rated by the usersTotal: 54 All time: 9,754 This week: 288Up
Version License PHP version Categories
mezon-router 1.0MIT/X Consortium ...7.2HTTP, PHP 7
Description Author

This package can route HTTP requests mapping URLs into classes.

It can take a given action handler class and create automatic routes by mapping request URLs based on the names of the class functions starting with the prefix action into action handler functions that are called when request URLs with the same names are called.

The package can also map given URL patterns to specific functions that handle the requests.

Name: Alexey Dodonov <contact>
Classes: 5 packages by
Country: Russian Federation Russian Federation
Age: ???
All time rank: 4013112 in Russian Federation Russian Federation
Week rank: 11 Up2 in Russian Federation Russian Federation Up
Innovation award
Innovation award
Nominee: 1x

Details

Routing Build Status Scrutinizer Code Quality codecov

Intro

Mezon provides simple routing class for your needs.

Installation

Just print in console

composer require mezon/router

And that's all )

Reasons to use

The mezon/router is more than 25 times faster then klein/klein router (in some cases).

results

Here you can see details

Simple routes

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' );

One handler for all routes

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.

Route variables

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.

Request types and first steps to the REST API

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 folder image Files  
File Role Description
Files folder imagedoc (6 files)
Files folder imageTests (5 files)
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation
Plain text file Router.php Class Class source
Plain text file RoutesSet.php Class Class source
Plain text file UrlParser.php Class Class source
Plain text file Utils.php Class Class source

  Files folder image Files  /  doc  
File Role Description
  Accessible without login Image file graph-klein.png Data Auxiliary data
  Accessible without login Image file graph-symfony.png Data Auxiliary data
  Accessible without login Plain text file router-symfony.md Data Auxiliary data
  Accessible without login Plain text file router.md Data Auxiliary data
  Accessible without login Image file table-klein.png Icon Icon image
  Accessible without login Image file table-symfony.png Data Auxiliary data

  Files folder image Files  /  Tests  
File Role Description
  Plain text file DynamicRoutesInvalidCasesUnitTest.php Class Class source
  Plain text file DynamicRoutesUnitTest.php Class Class source
  Plain text file RouterUnitTest.php Class Class source
  Plain text file RouterUtilsUnitTest.php Class Class source
  Plain text file StaticRoutesUnitTest.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:54
This week:0
All time:9,754
This week:288Up

For more information send a message to info at phpclasses dot org.