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.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package router

Routing

Latest Stable Version Open Collective Build Status Scrutinizer Code Quality codecov Twitter

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

More benchmarks can be found here.

Learn more

More information can be found here:

Twitter

dev.to

What is "First case" and "Second case"?

  1. 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.

  2. 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.

results

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:

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:

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.

Order of execution of the middlewares

  1. Global middlewares $router->addRoute('*', ...).
  2. 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:

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/.


All versions of router with dependencies

PHP Build Version
Package Version
Requires php Version >=7.4.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package mezon/router contains the following files

Loading the files please wait ....