


YII uses the url component to beautify and manage the method, yiiurl component beautification_PHP tutorial
YII uses the url component to beautify and manage, yiiurl component beautifies
This article describes the example of YII using the url component to beautify and manage. Share it with everyone for your reference, the details are as follows:
urlManager component
yii’s official documentation explains this as follows:
urlSuffix The url suffix used by this rule. By default, CurlManger::urlSuffix is used, and the value is null. For example, you can set this to .html to make the url look "like" a static page.
caseSensitive Whether it is case sensitive, the default is CUrlManager::caseSensitive, the value is null.
defaultParams The default get parameters used by this rule. When using this rule to parse a request, the value of this parameter will be injected into the $_GET parameter.
matchValue When creating a URL, whether the GET parameters match the corresponding sub-pattern. By default, CurlManager::matchValue is used, and the value is null.
If this attribute is false, it means that when the route and parameter names match the given rules, a URL will be created accordingly.
If this attribute is true, then the given parameter value must match the corresponding parameter sub-pattern.
Note: Setting this property to true will reduce performance.
We use some examples to explain how URLs work. Let's assume that our rules include the following three:
array( 'posts'=>'post/list', 'post/<id:\d+>'=>'post/read', 'post/<year:\d{4}>/<title>'=>'post/read', )
Call $this->createUrl('post/list') to generate /index.php/posts. The first rule applies.
Call $this->createUrl('post/read',array('id'=>100)) to generate /index.php/post/100. The second rule applies.
Call $this->createUrl('post/read',array('year'=>2008,'title'=>'a sample post')) to generate /index.php/post/2008/ a sample post. The third rule applies.
Call $this->createUrl('post/read') to generate /index.php/post/read. Please note that no rules apply.
In summary, when using createUrl to generate a URL, the route and GET parameters passed to the method are used to determine which URL rules apply. If each parameter in the association rule can be found in the GET parameter, it will be passed to createUrl. If the route rule also matches the route parameter, the rule will be used to generate the URL.
If the GET parameter passed to createUrl is one of the rules required above, the other parameters will appear in the query string. For example, if we call $this->createUrl('post/read',array('id'=>100,'year'=>2008)) , we will get /index.php/post/100? year=2008. To make these extra parameters appear as part of the path information, we should append /* to the rule. So, with the rule post/
As we mentioned, other uses of URL rules are to parse request URLs. Of course, this is a reverse process of URL generation. For example, when the user requests /index.php/post/100, the second rule of the above example will be applied to parse the route post/read and the GET parameter array('id'=>100) (available via $_GET).
Tip: This URL is a relative address generated by the createurl method. In order to get an absolute url, we can use the prefix yii: :app()->hostInfo, or call createAbsoluteUrl.
Note: The URL rules used will reduce the performance of the application. This is because when parsing the requested URL, [CUrlManager] tries to match it using each rule until a certain rule can apply. Therefore, high-traffic website applications should minimize the URL rules they use.
test.com/vthot wants to generate test.com/vthot/
Copy code The code is as follows: 'urlSuffix'=>'/',
To change the URL format, we should configure the urlManager application element so that createUrl can automatically switch to the new format and the application can correctly understand the new URL:
'urlManager'=>array( 'urlFormat'=>'path', 'showScriptName'=>false, 'urlSuffix'=>'.html', 'rules'=>array( 'posts'=>'post/list', 'post/<id:\d+>'=>array('post/show','urlSuffix'=>'.html'), 'post/<id:\d+>/<mid:\w+>'=>array('post/view','urlSuffix'=>'.xml'), ), ),
Example 1
Rule code
Copy code The code is as follows: 'posts'=>'post/list',
Action code
Copy code The code is as follows: echo $this->createAbsoluteUrl('post/list');
Output
http://localhost/test/index.php/post
Example 2
Rule code
Copy code The code is as follows: 'post/
Action code
Copy code The code is as follows: echo $this->createAbsoluteUrl('post/show',array('id'=>998, 'name'=>'123' ));
Output
http://localhost/test/index.php/post/998.html?name=123
Example 3
Rule code:
Copy code The code is as follows: 'post/
Action代码
复制代码 代码如下:echo $this->createAbsoluteUrl('post/view',array('id'=>998, 'mid'=>'tody'));
输出
http://localhost/test/index.php/post/998/tody.xml
示例四
Rule代码
复制代码 代码如下:'http://
Action代码:
echo $this->createAbsoluteUrl('look/host',array('user'=>'boy','mid'=>'ny-01')); echo ''; echo $this->createAbsoluteUrl('looks/host',array('user'=>'boy','mid'=>'ny-01'));
输出
http://boy.vt.com/look.me?mid=ny-01
http://localhost/test/index.php/looks/host/user/boy/mid/ny-01
1)controller/Update/id/23
public function actionUpdate(){ $id = Yii::app()->request->getQuery('id') ; 经过处理的$_GET['id'] } //$id = Yii::app()->request->getPost('id'); 经过处理的$_POST['id'] //$id = Yii::app()->request->getParam('id'); //CHttpRequest更多
2)public function actionUpdate($id) 这种不支持多主键,会检查一下到底GET里面有没有id,没有id就直接不允许访问
'sayhello/<name>' => 'post/hello', name是PostController actionHello($name)的参数 'post/<alias:[-a-z]+>' => 'post/view', domain/post/e文小写 其中:前面的alias是PostController actionView($alias)的参数 '(posts|archive)/<order:(DESC|ASC)>' => 'post/index', domain/posts/DESC或domain/posts/ASC '(posts|archive)' => 'post/index', domain/posts或domain/archive 'tos' => array('website/page', 'defaultParams' => array('alias' =>'terms_of_service')),
When the URL is /tos, pass terms_of_service as the alias parameter value.
隐藏 index.php
还有一点,我们可以做进一步清理我们的网址,即在URL中藏匿index.php 入口脚本。这就要求我们配置Web服务器,以及urlManager应用程序元件。
1.add showScriptName=>false
2.add project/.htaccess
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
3.开启rewrite
简单的说,在main.php中简单设置urlManager,然后讲了3条规则,基本都覆盖到了。最后是隐藏index.php,请记住.htaccess位于index.php同级目录 ,而不是protected/目录。其他就简单了。
希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。
您可能感兴趣的文章:
- YiiFramework入门知识点总结(图文教程)
- Yii入门教程之目录结构、入口文件及路由设置
- Yii入门教程之Yii安装及hello world
- Yii PHP Framework实用入门教程(详细介绍)
- Yii查询生成器(Query Builder)用法实例教程
- Yii实现单用户博客系统文章详情页插入评论表单的方法
- Yii中CGridView实现批量删除的方法
- Yii快速入门经典教程

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

AI Hentai Generator
Generate AI Hentai for free.

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

Many users always encounter some problems when playing some games on win10, such as screen freezes and blurred screens. At this time, we can solve the problem by turning on the directplay function, and the operation method of the function is also Very simple. How to install directplay, the old component of win10 1. Enter "Control Panel" in the search box and open it 2. Select large icons as the viewing method 3. Find "Programs and Features" 4. Click on the left to enable or turn off win functions 5. Select the old version here Just check the box

PHP function introduction—get_headers(): Overview of obtaining the response header information of the URL: In PHP development, we often need to obtain the response header information of the web page or remote resource. The PHP function get_headers() can easily obtain the response header information of the target URL and return it in the form of an array. This article will introduce the usage of get_headers() function and provide some related code examples. Usage of get_headers() function: get_header

The reason for the error is NameResolutionError(self.host,self,e)frome, which is an exception type in the urllib3 library. The reason for this error is that DNS resolution failed, that is, the host name or IP address attempted to be resolved cannot be found. This may be caused by the entered URL address being incorrect or the DNS server being temporarily unavailable. How to solve this error There may be several ways to solve this error: Check whether the entered URL address is correct and make sure it is accessible Make sure the DNS server is available, you can try using the "ping" command on the command line to test whether the DNS server is available Try accessing the website using the IP address instead of the hostname if behind a proxy

Vue is a very popular front-end framework. It provides many tools and functions, such as componentization, data binding, event handling, etc., which can help developers build efficient, flexible and easy-to-maintain Web applications. In this article, I will introduce how to implement a calendar component using Vue. 1. Requirements analysis First, we need to analyze the requirements of this calendar component. A basic calendar should have the following functions: display the calendar page of the current month; support switching to the previous month or next month; support clicking on a certain day,

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.

Differences: 1. Different definitions, url is a uniform resource locator, and html is a hypertext markup language; 2. There can be many urls in an html, but only one html page can exist in a url; 3. html refers to is a web page, and url refers to the website address.

Scrapy is a powerful Python crawler framework that can be used to obtain large amounts of data from the Internet. However, when developing Scrapy, we often encounter the problem of crawling duplicate URLs, which wastes a lot of time and resources and affects efficiency. This article will introduce some Scrapy optimization techniques to reduce the crawling of duplicate URLs and improve the efficiency of Scrapy crawlers. 1. Use the start_urls and allowed_domains attributes in the Scrapy crawler to

Win10 old version components need to be turned on by users themselves in the settings, because many components are usually closed by default. First we need to enter the settings. The operation is very simple. Just follow the steps below. Where are the win10 old version components? Open 1. Click Start, then click "Win System" 2. Click to enter the Control Panel 3. Then click the program below 4. Click "Enable or turn off Win functions" 5. Here you can choose what you want to open
