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
- User enters URL /
- Routing works on the first rule (
'/' => ['site', 'index']
), andSiteController::index()
is called - Everything is fine and good.
- Another situation is possible, the user enters the URL site / index
- Routing is triggered on the third rule (
'/[s:controller]/[s:action]' => ['{controller}', '{action}']
), and also callsSiteController::index()
- 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.
- 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