Home >PHP Framework >YII >How to configure pathinfo mode in yii framework

How to configure pathinfo mode in yii framework

angryTom
angryTomOriginal
2020-02-18 11:18:502311browse

How to configure pathinfo mode in yii framework

How to configure the pathinfo mode in the yii framework

After deploying an application built by the Yii framework for the first time, the framework does not use the PathInfo format by default URL, but in the form of http://yourdomain.com/index.php?r=account/login. This kind of URL is not only unsightly, but also not conducive to SEO, so here is how to use the PathInfo form in Yii. URL (Note: The development environment is based on wampserver2.4).

1) Open the protected/config/main.php configuration file and remove the comment from the following urlManager code:

'urlManager' => array(
    'urlFormat' => 'path',
    'rules' => array(
        &#39;<controller:\w+>/<id:\d+>&#39;=>&#39;<controller>/view&#39;,
        &#39;<controller:\w+>/<action:\w+>/<id:\d+>&#39;=>&#39;<controller>/<action>&#39;,
        &#39;<controller:\w+>/<action:\w+>&#39;=>&#39;<controller>/<action>&#39;,
    ),
),

2) After removing it, we can use something like http:// Use a URL in the form of yourdomain.com/index.php/controller/action to access the application, but then we have to hide the index.php in the middle;

Recommended related article tutorials: yii Tutorial

3) Add a file named .htaccess in the root directory of the application and write the following content:

Options +FollowSymLinks
IndexIgnore */*
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

4) Enable the rewrite module of apache, in httpd Find #LoadModule rewrite_module modules/mod_rewrite.so in .conf and remove the "#" in front;

5) Restart apache;

6) Continue to edit the main.php file, and add the Add an element to the array of urlManager:

&#39;urlManager&#39; => array(
    &#39;urlFormat&#39; => &#39;path&#39;,
    &#39;showScriptName&#39; => false, // 添加这一行
    &#39;rules&#39; => array(
        &#39;<controller:\w+>/<id:\d+>&#39;=>&#39;<controller>/view&#39;,
        &#39;<controller:\w+>/<action:\w+>/<id:\d+>&#39;=>&#39;<controller>/<action>&#39;,
        &#39;<controller:\w+>/<action:\w+>&#39;=>&#39;<controller>/<action>&#39;,
    ),
),

7) Complete!

For more yiiIntroduction to Programming technologies, please continue to pay attention to the PHP Chinese website! !​

The above is the detailed content of How to configure pathinfo mode in yii framework. For more information, please follow other related articles on the PHP Chinese website!

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