There is such a routing

'/' => ['site', 'index'], '/[s:action]' => ['site', '{action}'], '/[s:controller]/[s:action]' => ['{controller}', '{action}'], '/[s:module]/[s:controller]/[i:id]' => [ '{controller}', 'view', '{module}'], //$id - параметр передаётся в action '/[s:module]/[s:controller]/[s:action]/[i:id]' => ['{controller}', '{action}', '{module}'], //$id - параметр передаётся в action 

How to direct the URL to the desired Controller and Action is clear. But now the problem is to analyze the return URLs. And so that this implementation does not use a lot of resources, since it will most likely be called to generate a set of links on the pages.

Suppose you need to create links for an example:

  '/' => '/', 'site/index' => '/', 'site/login' => '/login', 'user/info' => '/user/info' 'user/profile/view/123' => '/user/profile/123', 'user/profile/edit/123' => '/user/profile/edit/123', 

examples

Suppose we enter the URL / and get to the site controller index screen, now on the basis of the same rule you need to do the reverse rules for redirect / rewriterule / reverseroute / alias I still don’t know what to call it. That is, it is necessary that when entering the URL / site / index we get to /

Suppose we enter the URL / user / profile / 123 we get into the user module on the profile controller screen view with the parameter id = 123. Similarly, from the routing rules, we need to redirect to / user / profile / when entering the URL / user / profile / view / 123 123

Etc. According to the routing rules described above.

I will try again

  1. User enters URL /
  2. Routing works on the first rule ( '/' => ['site', 'index'] ), and SiteController::index() is called
  3. Everything is fine and good.
  4. Another situation is possible, the user enters the URL site / index
  5. Routing is triggered on the third rule ( '/[s:controller]/[s:action]' => ['{controller}', '{action}'] ), and also calls SiteController::index()
  6. Then SEO comes, and he says, and why we have two URLs that give the same content, this is not comme il faut. Make a redirect to the main page, ie on / , and so on for all the rules described in the routing.
  7. I ask a question on StackOverflow how to do it with the routing rules described above. '/' => ['site', 'index'] ====> '/site/index' => '/'

A bit about the routing format just in case

The key is the condition by which the URL is processed.

  • s: - string
  • i: - integer

Value is an array consisting of

  • controller
  • action
  • module

Explicitly or in the form of {placeholder}, which is taken from the URL by mask in the key

  • one
    Do not understand what "return URLs" mean? To form the shortest url for current MCA? - Goncharov Alexander
  • Clarify the question, in its current form it is not very clear what and where to redo it. - user207618
  • @Other, There are a number of input and output data in the question, also described it all the same with words. - Ninazu
  • @ Alexander Goncharov, did not catch what the abbreviation MCA means? - Ninazu
  • I wanted to answer ... But it turned out that it is really incomprehensible ... - Qwertiy

1 answer 1

That is, after re-reading, I truncated - you have several routes leading to the same MCA (module controller action). You want to always have only one match URL <=> MCA - for example, to redirect, or give 404. I will add one more definition - a route, this is a template (can be a string, an array, a callback, and anything else). Certain URLs can be parsed into an MCA + parameter array.

It is done this way - the priority parameter is added to the routes, or it can by default be calculated from the string length of the route, for example. Next, after the bootstrap stage of the application, sort the routes by priority.

Suppose that you started the class SomeRouter for the router. This class must have at least two methods: parse (collect MCA from a URL string, may return false if the URL does not match a single route) and stringify (build MCA + other parameters in a URL string). This class contains routes (list, which is in question under the phrase "There is such a routing")

So, when parsing the route, there should be a code whose essence is approximately:

 $requestMCA = SomeRouter::parse($URL); if (($goodURL = SomeRouter::stringify($requestMCA))!=$URL){ return redirect($goodURL); } 

The main point of this - both URL parsing and URL harvesting - should work according to the predictable priorities of the routes : collection / analysis SomeRouter inside SomeRouter work according to priorities . At the first hit the assembly / disassembly cycle is interrupted and the result is returned.

UPD :
Usually, more specific routes have a higher priority, and less specific routes have a lower priority. For example, route / is the highest priority, and route /[s:module]/[s:controller]/[s:action]/* is the lowest priority.

  • That's just the crux of the matter is the method stringify, MCAP, I parsed - Ninazu
  • @Ninazu build code depends on the routing system. In your routing system - not enough priorities. And the parametrization is incomprehensible - I don’t understand the meaning of the right part of the route, I don’t understand if there are instructions that are mandatory / optional parameters. So then - in any framework there is an assembly, but in ZF2, for example, in the class of each route there are different methods of assembly - because the routes can be set differently. Your routes are similar to Zend\Mvc\Router\Http\Segment - I advise you to look at the code for the buildPath method. But there is a broader definition of the route. - Goncharov Alexander
  • In my case, the array of routes is already sorted from larger to smaller - Ninazu
  • @Ninazu, approx. Excuse me, but I’m not going to write the algorithm for constructing an url for MCA - this is an olympiad problem 9th grade) And in general, I described the concept to you. - Goncharov Alexander