I’m about to start on a large ongoing project and, having used Yii 1.1 a lot in the past, I’ve decided that I can use Yii2 now it’s not too far from a stable release .
Essentially it will be a client portal that controls all of their websites from a single codebase, so I’d need some sort of multi-domain config management as all sites/domains would ideally point to the same frontend Yii entry point (assuming I use the Yii2 advanced app).
I’d like to keep all of the code in separated modules that I can re-use between sites and turn the modules on/off depending on the domain. Environment settings for dev and prod would also need to be factored in - I don’t want to have to sacrifice the ability to use dev and prod configs but I would also like to avoid having too many config files to deal with.
For anyone experienced with the Yupe CMS, I quite like the module based approach they use and, apart from the lack of multi-domain support, is roughly what I want to achieve.
I wondered if anyone had any experience with multi-domain support for Yii, or had any advice on how I might best achieve this with Yii2?
Will it be as simple as treating each domain like a different environment or is there a better way?
Thanks for your quick reply amnah. Much appreciated.
Yes, I’m aware of the Yii2 advanced app, but I’m wondering if anyone has used the advanced app for multiple domain configs ALONGSIDE dev and prod environment support rather than just for providing separated development and production environments for a single site/domain?
Just curious if there might be anything I will need to be aware of, or could do in a better way, considering that there may be a large amount of domains involved and therefore lots of different environment folders that might become quite difficult to manage.
As I understand it, the advanced app does indeed handle multiple domains. Specifically, they have frontend and backend.
So for example, if you wanted http://mysite.com and http://admin.mysite.com, you could point one vhost to app/frontend/web and point another vhost to app/backend web.
If you want to work with one codebase (all your controllers/models) you can just put the bootstrap file (index.php) along with the assets directory in the www / public_html / etc. directories of the domains you want use and refer to the codebase in there.
Just point to the directory where the codebase / config is in, for example:
I’ll be dealing with anywhere up to 100+ domains so neither solution above will be practical as it will become fairly unmanageable. Ideally I need to point all domains to the same frontend index.php entry point and manage all domains from whatever subdomain/domain I decide to point to the backend entry point.
I’ve used a similar structure to the Yii2 advanced app in the past for Yii 1.1 and it became quite annoying from a development point of view due to the amount of config files and trying to remember where I’d put everything, so I was hoping that Yii2 might have a better approach. Modules also don’t play well with multiple entry point structures out of the box. Trying to share a single module across a frontend and backend separated environment can cause a few headaches.
I’m just going to have to experiment to see what works best and probably write a multi-domain config/module manager to handle everything nicely. This looks like a good starting point - http://www.yiiframework.com/extension/app-manager/
There is a 1.x extension for storing config settings in a database table and writing config files based on the data. It can be adapted with some effort to serve as a module for managing the config files for multiple Clients, as well as collections of modules. You will need to create a separate data table (eg., clients) and its PK can be used as a FK to associate your clients table with the table (eg., settings or config) used to store the sets of config settings. You can also use the clients table to populate a dynamic menu in your single admin interface to load configurations (clients).
To automatically load the appropriate client from a single domain entrance point, try find a copy of the old Postnuke CMS or The Xaraya CMS to see how it was handled there. The former method is simple to implement. The latter method supports multiple forms of site loading for More complex situations than you have. Follow their example and it will allow you to use a single set of application files for multiple subdomains operating from a single server. Its gets a bit more complicated if you are using load balancing or have subdomains or even domains running on separate servers, but you can handle this ithrough you client table by storing the full url to the client.
In your web directory, you can create subdirectories for storing the bootstrap loader ( index.php) for individual client sites. For the applications config directory, you can create subdirectories for storing the configuations of your clients.
Content data can be stored in the same tables for multiple clients by using the clients table PK as a FK for your subdomains.
It’s actually fairly easy to setup after you start working on a new project if you design the database from the ground up with support for handling multiple clients.
Never tried this with 100+ sites, but did so with 16 subdomains in 1.x and numerous times for other frameworks. If you can get two subdomains or domains working in this fashion, you have it made because adding more clients is just a matter of setting them up from admin.
maybe you could use ApplicationConfigBehavior in which you could check the domain name and by it set configuration values ?
for example:
namespace common\behaviors;
use Yii;
use yii\base\Behavior;
use yii\base\Application;
class ApplicationConfigBehavior extends Behavior
{
public function events()
{
return [
Application::EVENT_BEFORE_REQUEST => [$this, 'beforeRequest'],
Application::EVENT_AFTER_REQUEST => [$this, 'afterRequest'],
];
}
public function beforeRequest()
{
// check domain name and set configuration...
// Yii::$app->modules = ['something', 'something'];
// Yii::$app->language = 'doge';
// ...
return true;
}
public function afterRequest()
{
return true;
}
}
then in your config file (for example main.php) just add: