Yii's urlManager component configuration

不言
Release: 2023-03-25 12:30:02
Original
2999 people have browsed it

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

Yiis urlManager component configuration Simple understanding of components

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

  1. Open/frontend /web/index.php, see
    Yiis urlManager component configuration
    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.

  2. 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
    Yiis urlManager component configuration
    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.

  3. Jump to verdor\yiisoft\yiiYiis urlManager component configuration\base\Application.php. It turns out that the urlManager component specifies the class here.
    Yiis urlManager component configuration

    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

    Yiis urlManager component configuration

  4. 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 Yiis urlManager component configuration

    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.

  5. 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.
    Yiis urlManager component configuration

    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

Yiis urlManager component configuration Add a virtual host

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>
Copy after login

This step is not necessary ~

3 Each configuration function

Take the about action of requesting the Site controller as an example

  1. 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

  2. ##showScriptName:

    After completion, you can access it through

    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提示找不到页面
      Yiis urlManager component configuration这意味着,apache服务器找不到url请求的文件,按apache理解,省略~/web/下没有site目录,所以,想要实现隐藏入口脚本,还要在/frontend/web/下添加.htaccess文件,官方文档介绍,具体步骤如下:

      RewriteEngine onRewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule . index.php
      Copy after login
    1. .htaccess添加内容如下,意思是,如果请求的文件或目录找不到,就转到index.php

  3. enableStrictParsing

    • false [默认]:不启用严格解析路由,意思是,如果请求url与所有rules规则都不匹配的话,就按照默认的路由处理方式来处理,即按[模块/]控制器/动作,方式去解析url。

    • true:设为true后,当请求url与rules规则不匹配,就报错。

      如,通过http://frontend.advanced.com/site/about请求,得到Yii框架的报错提示
      Yiis urlManager component configuration

      这意味着,请求经过apache的转发,已经找到目的文件(入口脚本),目的文件运行过程中,没有得到期望参数(没传或验证不通过),因此Yii框架抛异常了

      注意与上面apache提示找不到页面区分~

  4. suffix: 后缀名,如设置为suffix => &#39;.html,需通过http://frontend.advanced.com/site/about.html才能访问到

  5. rules: 规则的配置就很复杂了,下面详讲。

4 rules配置

  1. 想访问Siteabout动作,要在rules里加

    &#39;site/about&#39; => &#39;site/about&#39;
    Copy after login

    其中,左边称为pattern,对应输入的url,右边为route,对应[模型/]控制器/动作。

  2. 如果不想为每个动作都加一个规则,可以这样

    &#39;<controller:\w+>/<action:\w+>&#39; => &#39;<controller>/<action>&#39;,
    Copy after login

    可以这样理解,左边,接收请求url的对应值,对它们作\w验证,即必须是字母或数字或下划线,以/site/about为例,验证通过,赋值给临时变量controller,action,右边使用,从而找到Site控制器的about动作。

  3. 同理,模块下的控制器动作也可以这样实现

    &#39;<module:\w+>/<controller:\w+>/<action:\w+>&#39; => &#39;<module>/<controller>/<action>&#39;,
    Copy after login
  4. restful的路由规则,在研究,日后再补~

  5. Yiis urlManager component configuration Notice

    • suffix 设了.html, 下面的rules都会用到,要想不用,需要在规则数组单独声明suffix=> ''

    • restful路由配置,pluralize参数默认为true,假如控制器为UserController,要通过users的url才能访问到,设为false的话,就不用加s,通过user即可访问,如果控制器本来就是UsersController,不管pluralize如何配置,都是通过users访问

    相关推荐:

    yii的CURD操作

    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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template