Yii2 重定向到另一个带有参数和 prettyURL 的操作


Yii2 Redirect to another action with parameter and prettyUrl

我有一个简单的操作来创建功能。

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,
        ]
    );
}

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

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

但是,当调用$this->redirect时,它会在不考虑上述配置的情况下打开 URL。所以,这就是我看到的:

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

而不是:

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

怎么了?

您需要在网址管理器上设置规则。

像这样:

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

据我了解,prettyUrl只是关于使用路径格式。

以西结菜鸟共享的学分。