Table of Contents
Yiis urlManager component configuration Simple understanding of components
Yiis urlManager component configuration Add a virtual host
3 Each configuration function
4 rules配置
Yiis urlManager component configuration Notice
Home Backend Development PHP Tutorial Yii's urlManager component configuration

Yii's urlManager component configuration

May 05, 2018 am 09:41 AM
components Configuration

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to set up Git configuration in PyCharm How to set up Git configuration in PyCharm Feb 20, 2024 am 09:47 AM

Title: How to correctly configure Git in PyCharm In modern software development, the version control system is a very important tool, and Git, as one of the popular version control systems, provides developers with powerful functions and flexible operations. As a powerful Python integrated development environment, PyCharm comes with support for Git, allowing developers to manage code versions more conveniently. This article will introduce how to correctly configure Git in PyCharm to facilitate better development during the development process.

The working principle and configuration method of GDM in Linux system The working principle and configuration method of GDM in Linux system Mar 01, 2024 pm 06:36 PM

Title: The working principle and configuration method of GDM in Linux systems In Linux operating systems, GDM (GNOMEDisplayManager) is a common display manager used to control graphical user interface (GUI) login and user session management. This article will introduce the working principle and configuration method of GDM, as well as provide specific code examples. 1. Working principle of GDM GDM is the display manager in the GNOME desktop environment. It is responsible for starting the X server and providing the login interface. The user enters

The perfect combination of PyCharm and PyTorch: detailed installation and configuration steps The perfect combination of PyCharm and PyTorch: detailed installation and configuration steps Feb 21, 2024 pm 12:00 PM

PyCharm is a powerful integrated development environment (IDE), and PyTorch is a popular open source framework in the field of deep learning. In the field of machine learning and deep learning, using PyCharm and PyTorch for development can greatly improve development efficiency and code quality. This article will introduce in detail how to install and configure PyTorch in PyCharm, and attach specific code examples to help readers better utilize the powerful functions of these two. Step 1: Install PyCharm and Python

Understand Linux Bashrc: functions, configuration and usage Understand Linux Bashrc: functions, configuration and usage Mar 20, 2024 pm 03:30 PM

Understanding Linux Bashrc: Function, Configuration and Usage In Linux systems, Bashrc (BourneAgainShellruncommands) is a very important configuration file, which contains various commands and settings that are automatically run when the system starts. The Bashrc file is usually located in the user's home directory and is a hidden file. Its function is to customize the Bashshell environment for the user. 1. Bashrc function setting environment

How to configure workgroup in win11 system How to configure workgroup in win11 system Feb 22, 2024 pm 09:50 PM

How to configure a workgroup in Win11 A workgroup is a way to connect multiple computers in a local area network, which allows files, printers, and other resources to be shared between computers. In Win11 system, configuring a workgroup is very simple, just follow the steps below. Step 1: Open the "Settings" application. First, click the "Start" button of the Win11 system, and then select the "Settings" application in the pop-up menu. You can also use the shortcut "Win+I" to open "Settings". Step 2: Select "System" In the Settings app, you will see multiple options. Please click the "System" option to enter the system settings page. Step 3: Select "About" In the "System" settings page, you will see multiple sub-options. Please click

Simple and easy-to-understand PyCharm configuration Git tutorial Simple and easy-to-understand PyCharm configuration Git tutorial Feb 20, 2024 am 08:28 AM

PyCharm is a commonly used integrated development environment (IDE). In daily development, using Git to manage code is essential. This article will introduce how to configure Git in PyCharm and use Git for code management, with specific code examples. Step 1: Install Git First, make sure Git is installed on your computer. If it is not installed, you can go to [Git official website](https://git-scm.com/) to download and install the latest version of Git

How to configure and install FTPS in Linux system How to configure and install FTPS in Linux system Mar 20, 2024 pm 02:03 PM

Title: How to configure and install FTPS in Linux system, specific code examples are required. In Linux system, FTPS is a secure file transfer protocol. Compared with FTP, FTPS encrypts the transmitted data through TLS/SSL protocol, which improves Security of data transmission. In this article, we will introduce how to configure and install FTPS in a Linux system and provide specific code examples. Step 1: Install vsftpd Open the terminal and enter the following command to install vsftpd: sudo

Angular components and their display properties: understanding non-block default values Angular components and their display properties: understanding non-block default values Mar 15, 2024 pm 04:51 PM

The default display behavior for components in the Angular framework is not for block-level elements. This design choice promotes encapsulation of component styles and encourages developers to consciously define how each component is displayed. By explicitly setting the CSS property display, the display of Angular components can be fully controlled to achieve the desired layout and responsiveness.

See all articles