Rumah  >  Artikel  >  rangka kerja php  >  yii框架怎么配置pathinfo的模式

yii框架怎么配置pathinfo的模式

angryTom
angryTomasal
2020-02-18 11:18:502105semak imbas

yii框架怎么配置pathinfo的模式

yii框架怎么配置pathinfo的模式    

第一次部署好Yii框架搭建的应用后,框架默认使用的不是PathInfo形式的URL,而是类似http://yourdomain.com/index.php?r=account/login 这样的形式,这种URL不仅不美观,而且不利于SEO,所以下面介绍在Yii中如何使用PathInfo形式的URL(注:开发环境基于wampserver2.4)。

1)打开protected/config/main.php配置文件,将下面这段urlManager代码的注释去掉:

'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)去掉以后,我们就可以使用类似http://yourdomain.com/index.php/controller/action这种形式的URL去访问应用,但是接下来我们还要隐藏掉中间的那个index.php;

相关文章教程推荐:yii教程

3)在应用的根目录下添加一个名为.htaccess的文件,并写入以下内容:

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)开启apache的rewrite模块,在httpd.conf中找到#LoadModule rewrite_module modules/mod_rewrite.so,把前面的“#”去掉;

5)重启apache;

6)继续编辑main.php文件,在刚才那个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)完成!

更多yii编程入门技术,请持续关注PHP中文网!!    

Atas ialah kandungan terperinci yii框架怎么配置pathinfo的模式. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel sebelumnya:yii怎么加载公共文件Artikel seterusnya:yii如何获取验证码值