Yii's urlManager component 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
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 ofApplication
, 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 forcomponents
, 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 thecoreComponents
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 theApplication
class, the constructor is automatically called! We found thatomitted~\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 onProbably 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 toComponent::__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 theurlManager
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
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>
This step is not necessary ~
3 Each configuration function
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
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提示找不到页面,这意味着,apache服务器找不到url请求的文件,按apache理解,
省略~/web/
下没有site
目录,所以,想要实现隐藏入口脚本,还要在/frontend/web/
下添加.htaccess
文件,官方文档介绍,具体步骤如下:RewriteEngine onRewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule . index.php
Copy after login在
.htaccess
添加内容如下,意思是,如果请求的文件或目录找不到,就转到index.phpenableStrictParsing
: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
: 规则的配置就很复杂了,下面详讲。
4 rules
配置
想访问
Site
的about
动作,要在rules里加'site/about' => 'site/about'
Copy after login其中,左边称为
pattern
,对应输入的url,右边为route
,对应[模型/]控制器/动作。如果不想为每个动作都加一个规则,可以这样
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
Copy after login可以这样理解,左边,接收请求url的对应值,对它们作
\w
验证,即必须是字母或数字或下划线,以/site/about
为例,验证通过,赋值给临时变量controller
,action
,右边使用,从而找到Site
控制器的about
动作。同理,模块下的控制器动作也可以这样实现
'<module:\w+>/<controller:\w+>/<action:\w+>' => '<module>/<controller>/<action>',
Copy after loginrestful的路由规则,在研究,日后再补~
suffix
设了.html
, 下面的rules都会用到,要想不用,需要在规则数组单独声明suffix=> ''
restful路由配置,
pluralize
参数默认为true,假如控制器为UserController
,要通过users
的url才能访问到,设为false的话,就不用加s,通过user
即可访问,如果控制器本来就是UsersController
,不管pluralize
如何配置,都是通过users
访问
Yiis urlManager component configuration Notice
相关推荐:
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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.

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

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

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

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

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

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.
