【问题标题】:Yii how to redirect to slug link form controller?Yii 如何重定向到 slug 链接表单控制器?
【发布时间】:2014-08-08 10:17:56
【问题描述】:

我有一个论坛,网址为:http://example.com/fourm/

在 url 中使用 slug,例如:http://example.com/fourm/a-category-name

分类页面有添加主题的表格, 哪个用户可以输入标题,这也将使其成为 SEO 的 slug 假设标题 slug 是“some-topic-title”

所以主题网址将是:http://example.com/fourm/a-category-name/some-topic-title

我的问题是,我应该在redirect() 函数中输入什么?

public function actionCreate(){
    if(isset($_POST['Forum'])){
        $model->attributes=$_POST['Forum'];
        if($model->save()){
            $this->redirect( ????? );
        }
    }

显示主题内容的控制器是“ForumPostController”,里面的actionIndex是这样的:

public function actionIndex($cateSlug, $TopicSlug){
    $model = new ForumPost;
    $cate_id  = ForumCate::model()->getCateIdBySlug():
    $topic_id = Forum::model()->getTopicIdBySlug():
    $model = $model->selectPost($cate_id, $topic_id);
    $this->render('index',array(
        'model'=>$model;
    ));
}

我知道我可以使 URL 重定向,但最好以“yii 方式”进行。 上面的操作用于处理用户输入链接,我应该采取另一个操作来接收重定向吗?或任何其他想法?

【问题讨论】:

    标签: php yii


    【解决方案1】:
    $this->redirect($this->createUrl('index', array('cateSlug' => $model->cate_slug)));
    

    参考这里:http://www.yiiframework.com/doc/api/1.1/CController#createUrl-detail

    【讨论】:

      【解决方案2】:

      您可以将数组作为参数添加到重定向函数。数组的第一个元素应该是控制器/动作,所有其他元素都是参数。参考这里:http://www.yiiframework.com/doc/api/1.1/CController#redirect-detail

      所以这对你有用:

      $this->redirect(array(
              'forumPostController/index',
              'cateSlug' => $model->cate_slug,
              'topicSlug' => $model->topicSlug,
      ));
      

      注意:确保在模型的 beforeSave 方法中生成 slug,因此在重定向用户的 $model->save() 之后,slug 将可用,并且您可以重定向正确。

      您不需要其他操作来处理重定向,只需重定向您呈现论坛主题的位置(所以它现在很好)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-10-06
        • 1970-01-01
        • 2014-07-04
        • 2013-12-31
        • 2012-03-08
        • 1970-01-01
        • 1970-01-01
        • 2014-10-18
        相关资源
        最近更新 更多