Download the PHP package mezon/router without Composer
On this page you can find all versions of the php package mezon/router. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package router
Short Description Small and fast routing framework
License MIT
Homepage https://github.com/alexdodonov/mezon-router
Informations about the package router
Routing
Intro
Mezon Framework provides simple routing class for your needs. It is already used in Web Application, Service, CRUD Service.
Contributors
Mezon becomes better because of the contributors. Thank them too.
Once again thank you people for your contributions.
Use this link if you also want to support our project.
Jaume |
Lolix |
Jamie Smith |
FAQ
Use this service for asking questions.
Installation
Just print in console
And that's all.
Reasons to use
The mezon/router is
- 25 times faster than klein/klein router
- 7 to 15 times faster than Symfony router
- 30 to 50 times faster than Laravel router
- 1.5 times faster than nikic/fast-route
More benchmarks can be found here.
Learn more
More information can be found here:
What is "First case" and "Second case"?
-
First case - http server accepts request, launches php script, wich handles this request, and then all script data uploads from memory. All following requests are processed in the same way. In this case very critical to launch script as soon as possible and we do not have time for long pre-compilations and preparations. Because all of it will be lost after the script will finish working.
- Second case - php script is launching, initiating all internal components (and router is one of them) and then starting processing requests. This case can be organized via for example react-php. It differs from the previous case because we can spend reasonable time to pre-compile routes for faster.
In this table you can see requests per second. The bigger numbers mean better.
mezon and klein comparison
mezon and symfony comparison
mezon and laravel comparison
mezon and fast-route comparison
mezon and yii2 router comparison
I'll be very glad if you'll press "STAR" button
Simple routes
Router allows you to map URLs on your php code and call when ever it needs to be called.
Router supports simple routes like in the example above - example.com/contacts/
Each Application object implicitly creates routes for its action[action-name]
methods, where action-name
will be stored as a route. Here is small (as usual) example:
And this code
will create router object and loads information about its actions and create routes. Strictly it will create two routes, because the class MySite has only two methods wich start with action[Suffix]
. Method someOtherPage
will not be converted into route automatically. By default this method will create routes wich handle both POST and GET request methods.
Then just call to run callback by URL:
There is a way to specify request methods for each action:
You can manually specify callbacks for every URL in your application:
And you also can use static methods:
We just need to create it explicitly.
We can also use simple functions for route creation:
And you can find callback without launching it:
Supported request methods
Mezon Router supports:
- GET
- POST
- PUT
- DELETE
- OPTION
- PATCH
To get the list of these methods you can use method getListOfSupportedRequestMethods:
One handler for all routes
You can specify one processor for all routes like this:
Note that routing search will stops if the *
handler will be found. For example:
In this example route /index/ will never be reached. All request will be passed to the *
handler. But in this example:
route /contacts/ will be processed by its own handler, and all other routes (even /index/) will be processed by the *
handler.
Route variables
And now a little bit more complex routes:
Here:
- i - any integer number
- a - any [a-z0-9A-Z_\/-.\@]+ string
- il - comma separated list of integer ids
- s - any string
Parameter name must consist of the following chars: [a-zA-Z0-9_-]
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:
Reverse routes
You can reverse routes and compile URLs by route's name. For example:
Routes caching
Since version 1.1.0 you can cache routes on disk and read them from this cache.
To dump cache on disk use:
And after that you can load routes:
But these methods have limitations - they can not dump and load closures because of obvious reasons.
You can also warm cache without dumping:
Middleware and parameters modification
Types of middlewares that you can add which will be called before the route handler will be executed. This middleware can transform common parameters $route and $parameters into something different.
- Multiple global middlewares that will be called in order of attachment.
- Multiple route specific middlewares that will be called in order of attachment.
Order of execution of the middlewares
- Global middlewares
$router->addRoute('*', ...)
. - Before calling route callback
$router->addRoute('/example', ...)
all those matching the route will be executed.
Let's look at a simple example:
Now let's watch an example with all the possibilities.
PSR-7 routes processing
Originally Mezon Router was not designed to be PSR-7 compatible. But one of the latest features have made it possible. You can use middleware for this purpose. For example:
The best thing about it - if you don't use PSR-7 in your project, then you don't "pay" for it.
Custom types
You can define your own types for URL parser. Let's try to create date
type.
First of all we should create simple class:
Here BaseType::PARAMETER_NAME_REGEXP is a global setting wich tells router that parameter names must consist of:
- a-z and A-Z letters
- 0-9
- and symbols _ and -
Now we need to define one more class method wich will parse date if it will occur:
And somewhere in your setup files you need to switch this type on:
Now you can handle routes like this:
But be careful. For example you will define such routes:
Then the first handler /posts-for-[date:posts-date]/
will be called for the route /posts-for-2020-02-02/
.