How better to solve this problem:

Here is the route in the usual CNC format:

htt://domain.com/user/list 

Where user is the controller and list is the action.

It is necessary at the beginning of each route to indicate the id of the entity with which the work is done, like so

 htt://domain.com/25/user/list 

And at the same time, it is necessary that controllers are created and actions are invoked according to the usual script, if the id of this entity ( 25 ) exists in the database.

UPDATE

25 is the project ID, not the user ID. And the route asks to give all the users of the project with ID = 25.

Another example:

 htt://domain.com/ru/user/list 

Or:

 htt://domain.com/en/user/list 
  • htt://domain.com/25/user/list - nobody does this. only perverts .... http://domain.com/user/25 - so they do and http://domain.com/users/page/25 do - Alexey Shimansky
  • @ Alexey Shimansky I didn’t understand the essence of the user at all. This route can be explained as follows: give me a list of all users of the project 25 - RostD

1 answer 1

In the site config (backend || frontend) /main.php you specify

 'urlManager' => [ 'showScriptName' => false, 'enablePrettyUrl' => true, 'rules' => [ '<locale:\w+>/<controller>/<action>' => '<controller>/<action>' ], ], 

When querying for http://domain.com/en/user/list/ will essentially send to

http://domain.com?r=user&action=list&locale=en

As <locale:\w+> you can write any name, for example <id:\d+> , this will be a variable for get requests. \ w + this means that any characters [a-zA-Z0-9]+ a \d+ are numbers [0-9]

  • And then in each action of each controller I will have to write the same code that defines the settings of the application language ... Not to mention that now all my rules will have <locale: \ w +> in the beginning Not very comfy ... - RostD
  • And you can make BaseController set up the necessary things, and inherit all your controllers from it and no duplication :) at the expense of all the other rules => it depends on how you arrange this rule, at the beginning all the general rules - then everything specific and everything will be good . But you have very specific requirements) revise the routing. - Sultanov
  • But after all, the processing of a locale variable should be in every action, eh? Then how will BaseController help me, if this should happen in every method? - RostD
  • In any case, thanks for the help, but for now the question remains open, because this is not the right decision from the point of view of the application architecture - RostD
  • one
    For multilingualism, there are other - better approaches, or try to finish UrlManager like this: habrahabr.ru/post/226931 but you can always slip your essences in front of the controller name. - Sultanov