Yii流程中的URL管理器

时间:2014-03-12 06:56:08

标签: php yii

我在Yii实施了项目。我有URL管理器配置问题。我成功完成了URL管理,但是当我渲染我想要显示的名称时,即

'urlManager'=>array(
        'urlFormat'=>'path',
        'rules'=>array(
                            'Home'=>'site/index',
            //'cuisine'=>'recipe/index3',

            'cuisine/<:\w+>/<id:\d+>/' => 'recipe/index3',
                            //'<id:\w+>/'=>'recipe/index3',
                            '<title:.*?>/p/<id:\d+>'=>'product/view',
            'holidays/<name:\w+>/<id:\d+>/'=>'recipe/index1',
            'calories/<name:\w+>/<id:\d+>/'=>'recipe/index2',

上面的代码工作正常。但食谱/指数3而不是美食。我想要表演。我给出了上述声明,然后我评论说因为它不起作用。 下一个声明工作正常。但在渲染页面时要明智地显示

http://kitchenking.ebhasin.com/recipe/index3/name/German

在上面的链接。想要像这样展示。

http://kitchenking.ebhasin.com/cuisine/German

2 个答案:

答案 0 :(得分:1)

你试过这个:

'cuisine/<name:\w+>/<id:\d+>/' => 'recipe/index3',

答案 1 :(得分:1)

从规则中删除ID:

'cuisine/<name:\w+>' => 'recipe/index3',

允许具有多个空格的标题的正确方法是使用另一个名为&#34; slug&#34;的字段,在此处复制标题并在保存之前将所有空格转换为短划线。使用slug作为URL而不是title。对此没有更简单的解决方案。

把它放在你的模型中:

protected function beforeSave() {
    $this->slug = preg_replace('/[^a-z A-Z]+/', '-', $this->title);
    return parent::beforeSave();
}

然后将此用于网址:

<a href="/cuisine/<?= $model->slug ?>"><?= $model->title ?></a>
相关问题