【问题标题】:How to write a URL rule in Yii framework?Yii框架中如何编写URL规则?
【发布时间】:2013-07-31 13:46:19
【问题描述】:
<?php echo CHtml::link($value->title, array(Yii::app()->createUrl('forum/thread', array('id'=>$value->thread_id)))); ?>

我有一个链接

论坛/线程/2

在我的 urlManager 规则中'thread/&lt;id:\d+&gt;' =&gt; 'forum/thread',

如何更改createUrl的规则和方法?

createUrl('any-value/forum/thread', array('id'=>$value->thread_id))

进入网址

forum/any-value/thread/2 或 forum/php-for-newbies/thread/2

对不起我的英语,非常感谢

【问题讨论】:

    标签: yii yii-routing yii-url-manager


    【解决方案1】:

    网址管理器规则应如下所示:

    'forum/<title:\w+>/thread/<id:\d+>' => 'forum/thread', //make sure this is listed first so it has priority
    'thread/<id:\d+>' => 'forum/thread',
    

    然后在你的控制器中你会有这个:

    public function actionThread($id,$title=null) {
        //$title will contain title from url if sent
    }
    

    【讨论】:

      【解决方案2】:

      试试这个: 'forum/any-value/thread/&lt;id:\d+&gt;' =&gt; 'any-value/forum/thread',

      还有这个: createUrl('any-value/forum/thread', array('id'=&gt;$value-&gt;thread_id)) 所以你应该得到forum/any-value/thread/2

      应该可以!

      但如果你在名为 forum 的模块内,那么你会这样做:

      'any-value/thread/&lt;id:\d+&gt;' =&gt; 'any-value/forum/thread',

      还有这个: createUrl('any-value/forum/thread', array('id'=&gt;$value-&gt;thread_id))

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-21
        相关资源
        最近更新 更多