Hello. It is necessary to implement such a controller. For example, there is a directory of phones http: //site.loc/phone when you click on a specific phone, it switched to http: //site.loc/phone/htc . That is, there is a phone controller, but the controller action is different each time, but the view is the same, only the content is substituted
- why so hard? - Sergalas
- The title of the question should not be the correct implementation of the controller ....... Tell me what are you trying to do? What a bicycle crutch bug?) - Alexey Shimansky
- Maybe they just need to save the url's application transfer to yii, the essence of the question is not why, but how - Daniel-664
- one@ Daniel-664 to answer directly "how" is not always correct if the action itself described in the question is wrong. Perhaps it is better to describe the essence of the problem in more detail and as a result there will be a more suitable solution than to plant cucumbers on an apple tree ..... It is better to write "I want to go from one continent to another" and not "How do I go along the bottom of the ocean and not suffocate " - Alexey Shimansky
- The formulation of the question is a mistake. Describe only the result you want to get. Do not try to offer your solutions - they contain an error, because with their help you could not get the desired result. - Razzwan
2 answers
For starters, we need an important and important digression:
For proper operation, you can configure urlManager as you need. What it is and how to work can be read about urlManager directly at the docks. Everything is well explained there: here or here or even in the office. docks
In brief, information can be viewed by opening this spoiler:
Если вкратце, в `urlManager` описываются правила в массиве как ключ -> значение. И вы там можете описать всё всё, что вашей душе угодно. Пример: `'posts' => 'post/index'` - соответствие URL `post` маршруту `post/index` А значит мы можем написать даже такой бред: `'user/index' => 'bread/bake'` - зайдя по URL решив посмотреть пользователя на самом деле обратимся к контроллеру Bread и методу Bake. То есть, чтобы теперь со страницы `category/ua1-1` уходил в `index` можно написать: 'urlManager'=>[ ... 'rules' => [ 'category/<id>' => 'category/index' ], ... ], При этом `<id>` можно описать регулярным выражением, если это требуется. Как пример: `<id:\d+>` - здесь id это только числа. А также не стоит забывать в контроллере дополнить входной параметр: public function actionIndex($id) Description taken from https://ru.stackoverflow.com/a/487553/191482
Now, taking into account the knowledge gained, we are doing your case:
Suppose there is a PhoneController controller, an actionTest method actionTest and this method takes 1 parameter.
As a result, in the urlManager , in the rules section we need to write a route
'urlManager'=>[ ... 'rules' => [ 'phone/<name:\w+>' => 'phone/test', ], ... ], All requests for mysite.ru/phone/MY_URL_PARAM_DATA will be redirected to phone/test and at the same time pass a parameter with the name array $_REQUEST
Now the controller:
<?php namespace app\controllers; class PhoneController extends \yii\web\Controller { public function actionTest($name) { return $this->render('test', ['myUrlParam' => $name]); } } In the controller, with a variable, you can do anything you want, substitute for selecting from the database or delete)) In this case, for the time being, just give the test variable that we took from Urla
Please note !!! that the name of the input parameter of the controller should also be called as it is called in the urlManager
Well, the presentation:
Это view/phone/test <br /> Мой параметр, переданный в урл: <?= $myUrlParam; ?> Everything.
If you need another parameter to accept, then add a rule in urlManager , for example:
'phone/<name:\w+>/<another:\w+>' => 'phone/test', and do not forget to add to the controller
public function actionTest($name, $another) {... If you have a different controller each time, you can simply render one template to render. But this is not the best way, will you have HtcController, NokiaController, etc?
You can not create different controllers, but use one controller, and if you want, even one action, but correctly create a routing map, on yii2 it is implemented in the config, in the section
'urlManager' => [ ... 'rules' => [ ... ] ], In the rules section, specify the necessary parameters, it can also be done differently: 'phone/htc' => 'phone/index' - specify one action handler for all brands, in this case you will have to update the rules when adding a new brand
'phone/<action>' => 'phone/<action>' - specify different actions, for different phones
'phone/<id:\d+>' => 'phone/index' - you can generally pass the phone brand id by the id parameter
The main thing is to make it more abstract, so that later there will be fewer problems with the expansion of the application.
- Include C
typein the example as well ... that typephone/<phoneTypePattern> => phone/indexso that you can take the phone brand in the action and by using it you could, for example, find something in the database - Alexey Shimansky - That's exactly what I need. Just how to implement it - Mykola Shkit
- @MykolaShkit This is just a job with
urlManagerand correct routing .... do you haveyiioryii2? These are (GROSSLY TALKING) different frameworks ..... and ifyii2then which template?baiscoradvanced? - Alexey Shimansky