Yii2-子域路由 [英] Yii2 - subdomain routing

查看:78
本文介绍了Yii2-子域路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用子域作为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;...
}

我什么都没得到-变量未打印,脚本停止工作(屏幕为白色)。当我删除$ 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

如何实现子域路由器?

How can I achieve subdomain router?

推荐答案

我认为您的路由器映射设置可以。如果您希望它更精确:

I think your router mapping setting is OK. If you want it to be more precise:

http://< user:[^ www] \w +>。 local.dev/<controller:\w+>/<action:\w+> => '< controller> /&action;>'

但是要使其正常工作,您最好仔细检查以下两件事:

But to make it work, you'd better double check following two things:

首先,您的虚拟主机应具有 *。local.dev

First, your virtual host should have a *.local.dev


  • 服务器名称 nginx

  • ServerAlias Apache

  • server_name in nginx
  • or ServerAlias in Apache

然后您可以将动态控制器的名称用作子域。

Then you can use dynamic controller's name as subdomain.

第二,您的虚拟主机应已配置为重写规则正确,请参考 Yii2文档

Second, your virtual host should have been configured rewrite rules correctly, refer to Yii2 doc.

例如对于 Apache ,只需在 YOUR_APP / web / .htaccess 文件$ c>包含以下内容行的文件夹:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆