📜 ⬆️ ⬇️

Fast PHP routing

image
Departing from the use of routing in the .htaccess file, first of all I came to the standard direction at index.php: I parsed the URL there and called the appropriate controllers - I was satisfied with this technique for a long time. However, recently I realized that I was doing something wrong, which could be done more efficiently and better.
Next, I will talk about my routing using XML for storing rules and subsequently using its serialized view.

Departing from checking in the index file, I wanted to get rid of such a construction first of all.
if ($url[1] == 'news'){} 


I didn’t really like to use constructs offered by most frameworks, which generally look like this:

 $route->addRule( '/news/{id}/', array( 'controler' = 'news', 'action' = 'showOne' ) ); 

')
Initially, I wanted to store the routing rules in JSON or XML.
However, parsing a file every time is not a good idea, and this type is more suitable for static navigation or navigation of the form / controller / action /.
I also wanted more flexibility in setting up routing and ultimately decided to use XML for storing rules, and after parsing a file and creating an array of rules, serialize it into a file (later using it to get settings)
The routing rules XML file looks like this:

 <root> <routes> <route match="exit" controller="user" action="exit" /> <route match="secret" controller="error" action="404"> <route match="love" controller="secret" action="love" /> </route> <route match="user" controller="user" action="list"> <route controller="user" action="user" match="{login}"> <route match="comments" controller="user" action="comments" /> <route match="wall" controller="user" action="wall" /> </route> </route> </routes> </root> 


The structure of the rule is as follows.
The XML rule element is contained in the / root / routes element, the rule element must contain the following attributes:
match - Used to search by URL
controller - the controller to be called
action - Called method

match can contain both static data, for example “secret”, and dynamic “page- {page | num}”, dynamic differ from static ones by the presence of curly brackets and the name of the variable in it (the name of the variable and its value will be obtained in case of coincidence)
In a variable, you can specify its type:
{param1 | num} - will only match if param1 is a numeric value.
{param2 | str} - will only match if param2 contains only letters and numbers

Based on XML, an array is formed that separates static and dynamic rules.
All descendants are also divided into static and dynamic.
Also in the / root / system element
The following data is stored:
  <route match="index" controller="index" action="index" /> <route match="not_found" controller="error" action="404" /> 

Accordingly, if there are no matches according to the rules of routing, a 404 error will be returned, in the case of an empty URL, its index value

Usage looks like this:

 $result = $router->get('/secret/love/'); 

The result will be:
 Array ( [controller] => secret [action] => love [values] => Array ( ) ) 


 $result = $router->get('/user/Testik/wall/'); 


 Array ( [controller] => user [action] => wall [values] => Array ( [login] => Testik ) ) 


BitBucket Sources
Download zip gz bz2

Source: https://habr.com/ru/post/134406/


All Articles