YII2 Redirect to backend after user registration f

2019-08-13 03:06发布

问题:

After installation of advance template in yii2, I got a user registration from at the frontend but I want it to redirect to backend after registration. How can that be done???

public function actionSignup()
{
    $model = new SignupForm();
    if ($model->load(Yii::$app->request->post())) {
        if ($user = $model->signup()) {
            if (Yii::$app->getUser()->login($user)) {
                return $this->goHome(); // I WANT TO CHANGE THIS TO REDIRECT TO LOCALHOST/MYAPP/BACKEND/WEB
            }
        }
    }

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

UPDATE here is the urlmanager

    'urlManager' => [
    'class' => 'yii\web\urlManager',
    'showScriptName' => false,
    ],
    'urlManagerBackend' => [
        'class' => 'yii\web\urlManager',
        'showScriptName' => false,
        'baseUrl' => 'http://localhost/ncddp/backend/web/index.php',
    ],

回答1:

You can configure separate urlManager component in frontend for backend:

'urlManager' => [
    'class' => 'yii\web\urlManager',
    'enablePrettyUrl' => true,
    'showScriptName' => false,
],
'urlManagerBackend' => [
    'class' => 'yii\web\urlManager',
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    'baseUrl' => 'http://admin.site.com',
],

Put in components section in application config.

Then you can use it like that:

Yii::$app->urlManagerBackend->createUrl(...);

Usage with redirect:

return $this->redirect(Yii::$app->urlManagerBackend->createUrl(...));

Related links:

  • Issue on Github
  • Url Manager component
  • Routing and URL Creation