动态创建子域
我正在使用 MVC 框架 (Yii) 创建一个网站,我需要动态创建子域,即 http:// company.website.com
因此,为了实现此目的,我添加了一个 DNS 通配符 (*.website.com)。现在的问题是我的应用程序控制器对于www.website.com 和company.website.com 都是相同的。例如,我有一个带有 Register
操作 (user/register
) 的 User
控制器。现在,如果我访问 www.website.com/user/register,我就可以注册,但如果我访问 company.website,我也可以完全相同进行同样的操作。 com/user/register。对于我的所有控制器来说,这种行为都是相同的。
我意识到一切正常,但如何分离 www.website.com 和 compnay.website.com 的控制器?我不希望用户从 subdomian url 访问注册/登录/其他控制器和操作。
任何建议将不胜感激!
谢谢你!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将主机名包含到路由规则数组中。例如,您可以创建规则
并检查
other/route
操作中的company
参数。请注意,这些规则需要http://
才能发挥作用。有关更多详细信息,请参阅 CUrlManager 文档。PS 如果
http://www.website.com
和http://company.website.com/user/register
的控制器完全不同,最好设置为这些网站创建两个应用程序。You could include hostname into your routing rules array. For example, you could create rules
and check for
company
parameter in yourother/route
action. Please note thathttp://
is required for those rules to work. See CUrlManager documentation for more details.P.S. If controllers for
http://www.website.com
andhttp://company.website.com/user/register
are completely different it could be better to set up two applications for those sites.如果我理解你的问题,URL 的“公司”部分是一个可变的公司名称。我将在这个假设下继续我的回答。
另一种选择是创建一个公司模块(我现在将其称为“公司”),并使用 CUrlManager 规则路由到该控制器。例如,
“company”字符串将作为 $_GET['company'] 传递到应用程序,您可以在 CompaniesModule.php 文件中使用此参数来加载一些公司特定的数据。
请注意,如果没有其他规则来处理 www.website.com 请求(根据 Gray Teardrop 的回答),您将在对该子域的请求中收到错误。
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.
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.