This article mainly introduces how YII uses the url component to beautify management. It analyzes the specific functions and related usage skills of the urlManager component in detail in the form of examples. Friends in need can refer to the examples in this article.
Describes how YII uses url components to beautify management. 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, CurlManger::urlSuffix is used by default, 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, CUrlManager::caseSensitive is used by default, and 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 the URL working rules. We 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. Therefore, 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 a 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 Want to generate test.com/vthot/
'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
'posts'=>'post/list',
Action code
echo $this->createAbsoluteUrl('post/list');
Output
http://localhost/test/index.php/post
Example 2
Rule code
'post/<id:\d+>'=>array('post/show','urlSuffix'=>'.html'),
Action code
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:
'post/<id:\d+>/<mid:\w+>'=>array('post/view','urlSuffix'=>'.xml'),
Action code
echo $this->createAbsoluteUrl('post/view',array('id'=>998, 'mid'=>'tody'));
Output
http://localhost/test/index.php/post/998/tody.xml
Example Four
Rule code
'http://<user:\w+>.vt.com/<_c:(look|seek)>'=>array('<_c>/host','urlSuffix'=>'.me'),
Action code:
echo $this->createAbsoluteUrl('look/host',array('user'=>'boy','mid'=>'ny-01')); echo ''; echo $this->createAbsoluteUrl('looks/host',array('user'=>'boy','mid'=>'ny-01'));
Output
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) This does not support multiple primary keys, it will check whether there is one in GET id, access is not allowed directly without 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/目录。其他就简单了。
以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!
相关推荐:
The above is the detailed content of How YII uses url components to beautify management. For more information, please follow other related articles on the PHP Chinese website!

Load balancing affects session management, but can be resolved with session replication, session stickiness, and centralized session storage. 1. Session Replication Copy session data between servers. 2. Session stickiness directs user requests to the same server. 3. Centralized session storage uses independent servers such as Redis to store session data to ensure data sharing.

Sessionlockingisatechniqueusedtoensureauser'ssessionremainsexclusivetooneuseratatime.Itiscrucialforpreventingdatacorruptionandsecuritybreachesinmulti-userapplications.Sessionlockingisimplementedusingserver-sidelockingmechanisms,suchasReentrantLockinJ

Alternatives to PHP sessions include Cookies, Token-based Authentication, Database-based Sessions, and Redis/Memcached. 1.Cookies manage sessions by storing data on the client, which is simple but low in security. 2.Token-based Authentication uses tokens to verify users, which is highly secure but requires additional logic. 3.Database-basedSessions stores data in the database, which has good scalability but may affect performance. 4. Redis/Memcached uses distributed cache to improve performance and scalability, but requires additional matching

Sessionhijacking refers to an attacker impersonating a user by obtaining the user's sessionID. Prevention methods include: 1) encrypting communication using HTTPS; 2) verifying the source of the sessionID; 3) using a secure sessionID generation algorithm; 4) regularly updating the sessionID.

The article discusses PHP, detailing its full form, main uses in web development, comparison with Python and Java, and its ease of learning for beginners.

PHP handles form data using $\_POST and $\_GET superglobals, with security ensured through validation, sanitization, and secure database interactions.

The article compares PHP and ASP.NET, focusing on their suitability for large-scale web applications, performance differences, and security features. Both are viable for large projects, but PHP is open-source and platform-independent, while ASP.NET,

PHP's case sensitivity varies: functions are insensitive, while variables and classes are sensitive. Best practices include consistent naming and using case-insensitive functions for comparisons.


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

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1
Powerful PHP integrated development environment

Atom editor mac version download
The most popular open source editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
