Answer the question
In order to leave comments, you need to log in
Yii2 how to implement a website with multiple cities?
Help, please, to make the implementation of the choice of cities on the site. It is necessary to make it so that the user can select a city and only the records of the selected city are shown to him. Cities are located in the database in the table 'city' and have columns 'id', 'name', 'url'
When choosing from the drop-down list, we get the selected city and substitute it in Url. It turns out something like this
www/site.ru/moscow/post/
www/site.ru/spb/post/
www/site.ru/moscow/user/
Answer the question
In order to leave comments, you need to log in
either write your own urlManager or form rules in rules like this:
and, accordingly, the city will be available in the controller, you can take the value of city in the config in on beforeAction and push it into yii::$app->params['city'], where you initially have the default city. Then it will be available anywhere in the code.
If you want to promote the site later, it is better to do subdomains. I know from personal experience that it will be much better and more profitable.
And so it is implemented through relational queries. Selection - redirect to the desired URL -> selection of records from the table, where the column city_id = city ID
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question