R
R
Roman2019-02-11 00:46:05
Yii
Roman, 2019-02-11 00:46:05

How to handle different domain names in Yii2?

The question is do I need on Yii2 Advanced to accept any hosts from the list in the database.
I have the main site mainSite.com, I want to make it possible to access it from different hosts (which will first be created and recorded in the database). The dns host will have a record of my main site IP (mainSite.com)
When accessing mainSite.com, the site works as it worked.
When accessing newSite.com, we look in the database if there is such a domain, after which it opens my own mainSite.com (just change the logo, for example).
The hosting is configured so that it can accept and process requests from any domain.
Does anyone know how to do this correctly?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
grinat, 2019-02-11
@grinat

Throw methods into the component that would allow you to determine what kind of subdomain it is, what options it has, etc., then it was convenient to access it everywhere through $yii::app->myDomainsComponent. When initializing this component, subscribe to request events and if, for example, the domain is incorrect, then redirect the person or throw exceptions, if correct, then write down all the crap that will be needed further in controllers, views, models.

M
Maxim Timofeev, 2019-02-11
@webinar

In config

'on beforeAction' => function($event){
      //Ваша логика. дергаем базу и т.д. Можно признак домена в params записывать, например
    },

Alternatively, if the difference is not only in the logo, but in a completely different view, then you can immediately change the theme and customize sites through themes: https://www.yiiframework.com/doc/guide/2.0/en/outp...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question