Yii url路由

时间:2014-09-08 07:08:13

标签: yii routing

我有一个url example.com/information/london。当有人调用此url时,我想调用控制器信息及其索引方法。但我想通过slug作为伦敦的工作,例如example.com/information/jobs-in-london,我怎么能通过在config / main.php中编写url规则来实现这个目标。 即我想将我的页面example.com/information/london重定向到example.com/information/jobs-in-london但不想使用.htaccess我想通过url路由规则来实现这一点我已经通过编写< / p>

'<_c:(information)>/<slug:london>'=>'information/index/jobs-in-london' 

但这对我不起作用。

class InformationController extends Controller
{


    public function actionIndex($slug)
    {

        CVarDumper::dump($slug,10,true);
        exit;

    }





}

2 个答案:

答案 0 :(得分:0)

你的问题不清楚。您可能意味着以下任何一种情况:


地址:

example.com/information/london
example.com/information/singapore

规则:

'information/<slug>' => 'information/index',

控制器:

public function actionIndex($slug)
{
    $slug = "jobs-in-".$slug;

    var_dump($slug);
}

Slug结果:

jobs-in-london
jobs-in-singapore

地址:

example.com/information/jobs-in-london
example.com/information/jobs-in-singapore

规则:

'information/<slug:jobs-in-.+>' => 'information/index',

控制器:

public function actionIndex($slug)
{
    var_dump($slug);
}

Slug结果:

jobs-in-london
jobs-in-singapore

答案 1 :(得分:0)

以下是您在路线中必须要做的事情:

'<controller:(information)>/<slug:>' => '<controller>/index'

以下是在创建URL时添加slug的方法:

Yii::app()->createUrl('information/index', array('slug' => 'jobs-in-london'));

并检查slu is是什么,你对该功能所做的是正确的。