问题
I'm creating a website using MVC framework (Yii) and I need to dynamically create subdomains, i.e. http://company.website.com
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 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 for www.website.com and for compnay.website.com? I don't want users to access register/login/other controllers and actions from the subdomian url.
Any suggestions are greatly appreciated!
Thank you!
回答1:
You could include hostname into your routing rules array. For example, you could create rules
array(
'http://www.website.com/user/register' => 'user/register',
'http://<company:\w+>.website.com/user/register' => 'other/route',
)
and check for company
parameter in your other/route
action. Please note that http://
is required for those rules to work. See CUrlManager documentation for more details.
P.S. If controllers for http://www.website.com
and http://company.website.com/user/register
are completely different it could be better to set up two applications for those sites.
回答2:
If I understand your question, the 'company' component of the URL is a variable company name. I'll continue my answer under that assumption.
Another option would be to create a company module (I'll call it 'Companies' for now), and use the CUrlManager rules to route to that controller. E.g.
array(
'http://<company:\w+>.website.com/user/register' => '/companies/user/register',
'http://<company:\w+>.website.com/<_c:\w+>/<_a:\w+>' => '/companies/<_c>/<_a>' // more generic option
),
The 'company' string will be passed to the application as $_GET['company'] and you can use this parameter in your CompaniesModule.php file to load some company specific data.
Please note that without some other rule to handle www.website.com requests (as per Grey Teardrop's answer) you will get errors on requests to that subdomain.
来源:https://stackoverflow.com/questions/3390533/dynamic-creation-of-subdomains