This article mainly introduces the configuration of the urlManager component of Yii, which has certain reference value. Now I share it with you. Friends in need can refer to it.
The configuration components mainly include:
Specify the class. If missing, the default class
attribute is used. If missing, use the corresponding attributes of the default class
urlManager is a class, why is it called a component? Don't worry about it for now, we just need to know that you need to specify a class for the component. If not specified, an error will be reported, unless the component has a default class. Which components have default classes? It is the core component. Install B and look at the source code. Take the yii advanced template as an example.
Pis: Unless otherwise specified, the path below refers to the specific path of the file, not the namespace
Open/frontend /web/index.php
, see
First, use the merge method of the array helper class to recursively merge the arrays, the last one overwrites the previous one Output the final configuration, then pass the configuration data to the constructor of Application
, and then execute its run method.
Jump to the run method of verdor\yiisoft\yiiYiis urlManager component configuration\web\Application.php
. It triggers many events. I won’t look at the details. It seems irrelevant. ~, where are the components related stuff? Searching for components
, it was found that the coreComponents method was executed, and its content was
The default class of urlManager was not found, and each configuration file did not specify a class for urlManager. Why was no error reported? Because it calls the coreComponents
method of the parent class.
Jump to verdor\yiisoft\yiiYiis urlManager component configuration\base\Application.php
. It turns out that the urlManager component specifies the class here.
We already know that we specify the class of core components through coreComponents
, but how does it call this method? Thinking back, what else did you do in the entry script? When instantiating the Application
class, the constructor is automatically called! We found that omitted~\web\Application
did not override the construction method of the parent class, that is, look at the construction method of the parent class
Jump to the preInit
method. In this pre-initialization method, the parameters accepted are references, that is, this method needs to transform the $config
array. Focus on
Probably means, If the configuration file does not configure a certain component or does not specify a class for a certain component, this component will use the class specified by
coreComponents. After constructing the $config
variable, pass it to Component::__construct($config)
to start the specific content of the component and not go any further.
Let’s look at the configuration of the component properties. Jump to /Project Directory/frontend/config/main.php
. We see that the urlManager component configuration is commented out. This means that it uses the default value of the attribute of the specified class of the urlManager
component, specifically in \vendor\yiisoft\yiiYiis urlManager component configuration\wbe\UrlManager.php
.
To summarize: component configuration, first specify the class (if there is no default, it must be stated in the configuration file), second configuration attribute, the attribute is the member variable of the class
For convenience, add a virtual host to the front-end project first, see the link for details
<VirtualHost *:Yiis urlManager component configuration0>DocumentRoot "${INSTALL_DIR}/www/advanced/frontend/web/"ServerName frontend.advanced.com</VirtualHost>
This step is not necessary ~
Take the about
action of requesting the Site
controller as an example
enablePrettyUrl
:
Pis: Set this to false, the following settings will not work
false [Default]: Access through entry script?r=[module/]controller/action mode. That is, http://localhost/advanced/frontend/web/index.php?r=site/about
true: Turn on beautification routing, (note that this is only Configure this to true, leave the others unconfigured, that is, use the default), and access it through the entry script/[module/] controller/action mode. i.e. http://localhost/advanced/frontend/web/index.php/site/about
##showScriptName:
http://frontend.advanced.com/site/about
true [默认]: 不隐藏入口脚本,即要加入口脚本文件名index.php才能访问到,http://localhost/advanced/frontend/web/index.php/site/about
false:按理解,设为false,应该是http://localhost/advanced/frontend/web/site/about
即可访问,但发现是apache提示找不到页面,
这意味着,apache服务器找不到url请求的文件,按apache理解,省略~/web/
下没有site
目录,所以,想要实现隐藏入口脚本,还要在/frontend/web/
下添加.htaccess
文件,官方文档介绍,具体步骤如下:
RewriteEngine onRewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule . index.php
在.htaccess
添加内容如下,意思是,如果请求的文件或目录找不到,就转到index.php
enableStrictParsing
:
false [默认]:不启用严格解析路由,意思是,如果请求url与所有rules
规则都不匹配的话,就按照默认的路由处理方式来处理,即按[模块/]控制器/动作,方式去解析url。
true:设为true后,当请求url与rules
规则不匹配,就报错。
如,通过http://frontend.advanced.com/site/about
请求,得到Yii框架的报错提示
这意味着,请求经过apache的转发,已经找到目的文件(入口脚本),目的文件运行过程中,没有得到期望参数(没传或验证不通过),因此Yii框架抛异常了。
注意与上面apache提示找不到页面区分~
suffix
: 后缀名,如设置为suffix => '.html
,需通过http://frontend.advanced.com/site/about.html
才能访问到
rules
: 规则的配置就很复杂了,下面详讲。
rules
配置想访问Site
的about
动作,要在rules里加
'site/about' => 'site/about'
其中,左边称为pattern
,对应输入的url,右边为route
,对应[模型/]控制器/动作。
如果不想为每个动作都加一个规则,可以这样
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
可以这样理解,左边,接收请求url的对应值,对它们作\w
验证,即必须是字母或数字或下划线,以/site/about
为例,验证通过,赋值给临时变量controller
,action
,右边使用,从而找到Site
控制器的about
动作。
同理,模块下的控制器动作也可以这样实现
'<module:\w+>/<controller:\w+>/<action:\w+>' => '<module>/<controller>/<action>',
restful的路由规则,在研究,日后再补~
suffix
设了.html
, 下面的rules都会用到,要想不用,需要在规则数组单独声明suffix=> ''
restful路由配置,pluralize
参数默认为true,假如控制器为UserController
,要通过users
的url才能访问到,设为false的话,就不用加s,通过user
即可访问,如果控制器本来就是UsersController
,不管pluralize
如何配置,都是通过users
访问
相关推荐:
The above is the detailed content of Yii's urlManager component configuration. For more information, please follow other related articles on the PHP Chinese website!