0

这是我的设置:

配置文件

 'urlManager'=>array(
          'urlFormat'=>'path',
          'rules'=>array(
           '<controller:\w+>/<id:\d+>'=>'<controller>/view',
           '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
           '<controller:\w+>/<action:\w+>'=>'<controller>/<action>'
          ),
          'showScriptName'=>false,
 ),

.htaccess

Options +FollowSymlinks
#+FollowSymLinks must be enabled for any rules to work, this is a security
#requirement of the rewrite engine. Normally it's enabled in the root and we
#shouldn't have to add it, but it doesn't hurt to do so.

RewriteEngine on
#Apache scans all incoming URL requests, checks for matches in our
#.htaccess file 
#and rewrites those matching URLs to whatever we specify.

#allow blank referrers.
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?site.com [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?site.dev [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?dev.site.com [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ - [NC,F,L]

# 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

在布局菜单上我有这个:

$this->widget('zii.widgets.CMenu', 
    array('items'=>
        array(
            array(
                'label'=>Yii::t('site','A'),
                'url'=>array('/site/index')
            ),
            array(
                'label'=>Yii::t('site','Q'),
                'url'=>array('rooms/index')
            ),
            array(
                'label'=>Yii::t('site','G'),
                'url'=>array('gastronomy/index')
            ),
            array(
                'label'=>Yii::t('site','A'),
                'url'=>array('activity/index')
            ),
            array(
                'label'=>Yii::t('site','S'),
                'url'=>array('services/index')
            ),
            array(
                'label'=>Yii::t('site','C'),
                'url'=>array('contacts/index')
            ),
            array(
                'label'=>Yii::t('site','R'),
                'url'=>array('booking/index')
            )
        )
    )
);

我在这里显式调用索引,因为看起来,显式调用它是必需的。

有了这个设置,每次我点击我得到的那些链接,例如:

http://site.dev/rooms/index

虽然我希望得到:

http://site.dev/rooms/

没有索引名称。

我在这里想念什么?

4

1 回答 1

3

索引条目文件和默认操作之间存在差异。你在搞砸这些事情。如果您这样做'showScriptName'=>true,您会看到,您的链接将更改为索引条目文件/index.php/rooms/index在哪里。index.php

正如您在选项中看到的那样,您的链接中'showScriptName'=>false没有该选项index.php,这意味着您已成功从链接中删除了条目脚本。

现在您room/indexcontroller/actionURL 路由的一部分。room是控制器,index是动作。要查看http://site.dev/rooms/而不是http://site.dev/rooms/index您必须像这样编辑您的 URL 路由:

'urlManager'=>array(
    'urlFormat'=>'path',
    'rules'=>array(
        '<controller:\w+>/<id:\d+>'=>'<controller>/view',
        '<controller:\w+>'=>'<controller>/index',
        '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
        '<controller:\w+>/<action:\w+>'=>'<controller>/<action>'
    ),
    'showScriptName'=>false,
),

注意'<controller:\w+>'=>'<controller>/index'我添加的行。这使得默认操作index创建controller路由而不是controller/index.

于 2012-09-03T15:24:37.430 回答