So, in order to achieve this I’ve added a DNS wildcard (*.website.com). Now the problem is that my application controllers are all the same for www.website.com and for company.website.com. For example, I have a User controller with Register action (user/register). Now if I go to www.website.com/user/register I can register, but I can do exactly the same thing if I go to company.website.com/user/register. And this behaviour is the same for all my controllers.
I realize everything is working correctly, but how do I separate controllers (or something like that) for www.website.com and for company.website.com? I don’t want users to access register/login/other_controllers_and_actions from the subdomian url.
This works good, but is not very dynamic. You may create a special method/component if you need more advanced url handling. Check link in my signature about i18n. So you can get an idea of how you can archive it.
I need subdomains to be dynamically created for every registered company (there will be more than 1000 companies) and this has to be one application (pretty much like tumblr.com works - subdomains for every user). So, there is no way I can create virtual hosts for each of them
@Y!!, thanks for your suggestion, that’s exactly how I’m tring to do (Parameterizing Hostnames). Everything works fine in my application, besides that some controllers+actions have to be accessed only from a subdomain (i.e. company.website.com/clients - ClientsController) and others have to be accessed only from the main website (i.e. www.website.com/user/register).
Ok, I’ve spent some time and came up with a very ugly (at least it seems to me so) solution.
I’ve created a new CApplicationComponent class (UrlAccess)
Added to my config file:
'preload'=>array('log', 'urlAccess')
and in the "components" section:
'urlAccess'=>array(
'class'=>'UrlAccess',
)
Now, when the application loads it first checks my new class (UrlAccess). And this what I have in the UrlAccess:
class UrlAccess extends CApplicationComponent {
public function init() {
// controllers allowed to be accessed from subdomains
$subdomainAllowed = array('manage', 'clients', 'etc.');
// controllers allowed to be accessed from the main domain
$domainAllowed = array('user', 'site', 'etc.');
// check if the path has "controller/action" pattern and assign the first element to $controller
// i.e. company.website.com/clients/new
if(strpos(request()->pathInfo, '/')) {
$pathInfo = explode('/', request()->pathInfo);
$controller = $pathInfo[0];
}
// if the path has only controller and no action specified (i.e. company.website.com/clients)
else {
$controller = request()->pathInfo;
}
$urlExplode = explode('.', $_SERVER['HTTP_HOST']);
// check if this is a subdomain
if(count($urlExplode) > 2 && $urlExplode[0] !== 'www') {
// check if the controller specified in the path is allowed to be accessed from subdomain
if(in_array(strtolower($controller), $subdomainAllowed)) {
return true;
}
else {
throw new CHttpException('404', 'Page not found');
}
}
// etc.
}
// etc.
}
This is obviously not the whole thing, just to give you a basic idea how it works now. Now if the user goes to: