M
M
Maxim2017-10-03 16:33:41
Yii
Maxim, 2017-10-03 16:33:41

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/

It is also required to get the selected city and enter it into the $city variable, which we can use when fetching data from the 'post' table with the fields 'city_id' == $city

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Timofeev, 2017-10-26
@myks92

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.

N
Nikolay Kaspor, 2017-10-03
@this_nicco

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 question

Ask a Question

731 491 924 answers to any question