Yii1 中的 URL 管理器和 Htaccess

Tri*_*adi 5 php .htaccess yii1.x

我有这样的网址:

www.studyenglish/index.php?r=site/lesson&act=read&id=1

我希望更改为:

www.studyenglish/site/lesson/read

我已在 url 管理器 config/main.php 中添加了此脚本

'urlManager'=>array(
       ....
       'showScriptName'=>false,
        ....
    ),
Run Code Online (Sandbox Code Playgroud)

并将此脚本添加到 .htaccess 中

Options +FollowSymLinks
RewriteEngine on
RewriteBase /

# 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
Run Code Online (Sandbox Code Playgroud)

但它只适用于这样的 url:

www.studyenglish/index.php?r=site/lesson

成为:

www.studyenglish/site/lesson

那么,如何更改此网址

www.studyenglish/index.php?r=site/lesson&act=read&id=1

成为

www.studyenglish/site/lesson/read

希望有人可以提供帮助。谢谢 ...

anw*_*erj 1

UrlManager中有规则,你可以定义自己的规则。你的 UrlManager 可能看起来像这样。

'urlManager' => array(
'urlFormat' => 'path',
    'rules' => array(
        'gii' => 'gii/index',
        'gii/<controller:\w+>/<action:[\w-]+>' => 'gii/<controller>/<action>',
        '<controller:\w+>/<id:\d+>' => '<controller>/view',
        '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
        'site/lesson/<act:\w+>/<id:\d+>' => 'site/lesson'
        //
Run Code Online (Sandbox Code Playgroud)

这将调用SiteController中的actionLesson,它应该获取两个参数。

你的方法应该看起来像这样。

public function actionLesson($act,$id){
    //
}
Run Code Online (Sandbox Code Playgroud)