【问题标题】:Yii cleaning URLsYii 清理 URL
【发布时间】:2013-04-29 01:22:13
【问题描述】:

我有这个网址:

http://mysite.com/pages/index?slug=how-does-it-work

我想把那个网址改成这样:

http://mysite.com/how-does-it-work

如您所见,pageindex 已在 url 中删除。 这里的页面是一个控制器,而索引是控制器的动作。 这是我的页面控制器上的代码:

public function actionIndex($slug)
{
   if(!empty($slug))
   {
    $pagesInfo = Pages::model()->find(array("condition"=>"slug='$slug'"));
    $isAdmin = 0;
    if(isset(Yii::app()->user->idUser)){
       $isAdmin = Users::model()->findByPk(Yii::app()->user->idUser)->isAdmin;
    }

    if($pagesInfo !== null){
      $this->render('index',array(
        'pagesInfo'=>$pagesInfo,
        'isAdmin'=>$isAdmin,
       ));
    }else{
       throw new CHttpException(404,'Page cannot be found');
    }
   }
}

这也是我的.htaccess

IndexIgnore */*
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

老实说,我不知道如何将该 URL 转换为我预期的 URL 输出。我知道这就是为什么我没有发布我所做的事情。

您的帮助将不胜感激!谢谢! :)

【问题讨论】:

    标签: php yii


    【解决方案1】:

    你应该简单地修改你的 urlManager 配置,例如:

    'urlManager'=>array(
        'urlFormat'=>'path',
        'showScriptName'=>false,
        'rules'=>array(
            '<slug>'=>'page/index',
    
            // ... other rules
        ),
    ),
    

    http://www.yiiframework.com/doc/guide/1.1/en/topics.url

    【讨论】:

      【解决方案2】:

      您是否已经在项目的 config/main.php 中将“urlFormat”更改为“path”?

      你想如何调用这个动作?

      【讨论】:

      • Elson,当您访问第二个 URL 时会发生什么?修改后,第一个还能用吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-22
      • 1970-01-01
      • 1970-01-01
      • 2017-02-06
      • 2017-01-27
      • 1970-01-01
      相关资源
      最近更新 更多