Yii2 - 子域路由 [英] Yii2 - subdomain routing
问题描述
我想使用子域作为 id,我需要动态路由器来做到这一点.在 urlManager 中,我添加了这一行:
I want to use subdomain as id, and I need dynamic router to do this. In urlManager, I added this line:
"http://<user:w+>.local.dev/<controller:w+>/<action>" => '<controller>/<action>',
当我尝试任何操作时,例如:
When I try any action, for example:
function actionMyAccount($user){
echo $user;...
}
我什么也没收到 - 没有打印 var,脚本停止工作(屏幕为白色).当我删除 $user 时,页面加载没有任何问题
I am not getting anything - the var isn't printed, and script stops working (screen is white). When I remove $user, the page is loading without any problems
如何实现子域路由器?
推荐答案
我觉得你的路由器映射设置没问题.如果您希望它更精确:
I think your router mapping setting is OK. If you want it to be more precise:
"http://
但是为了让它工作,你最好仔细检查以下两件事:
But to make it work, you'd better double check following two things:
首先,你的虚拟主机应该有一个 *.local.dev
First, your virtual host should have a *.local.dev
server_name
在nginx
- 或
Apache
中的
ServerAlias
然后您可以使用动态控制器的名称作为子域.
Then you can use dynamic controller's name as subdomain.
其次,你的虚拟主机应该已经正确配置了rewrite
规则,参考Yii2 文档.
Second, your virtual host should have been configured rewrite
rules correctly, refer to Yii2 doc.
例如对于 Apache
,只需在 YOUR_APP/web/
文件夹下创建一个 .htaccess
文件,内容如下:
e.g. for Apache
, just create a .htaccess
file under YOUR_APP/web/
folder with following content lines:
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
这篇关于Yii2 - 子域路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!