简体   繁体   English

Yii2使用参数和prettyUrl重定向到另一个动作

[英]Yii2 Redirect to another action with parameter and prettyUrl

I have this simple action for the creating functionality. 我具有创建功能的简单操作。

public function actionCreate()
{
    $model = new Horse();
    $model->attributes = \Yii::$app->request->post('Horse');

    if ((\Yii::$app->request->post()) && ($model->validate())) {
        $model->save(false);
        $this->redirect(
            [
                'view',
                'id' => $model->id
            ]
        );
    }

    return $this->render(
        'create',
        [
            'model' => $model,
        ]
    );
}

Under common/config/main.php , I defined: common / config / main.php下 ,我定义了:

'urlManager' => [
    'enablePrettyUrl' => true,
    'showScriptName' => false,
],

But, when the $this->redirect is called, it opens the URL without considering the above configuration. 但是,当调用$this->redirect ,它将打开URL,而无需考虑上述配置。 So, this is what I see: 所以,这就是我看到的:

http://traditionalbox.back.dev/horse/view?id=11

instead of: 代替:

http://traditionalbox.back.dev/horse/view/11

What's wrong? 怎么了?

You need to setup the Rules on your url manager. 您需要在网址管理器上设置规则。

Something like: 就像是:

'rules' => array(
                '<controller:\w+>/<id:\d+>' => '<controller>/view',
                '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
                '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
        ),

As far as I understand, the prettyUrl is just about using the path format. 据我了解, prettyUrl只是使用路径格式。

Credits shared with ezekielnoob . ezekielnoob分享的积分

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM