YII2 Redirect to backend after user registration from frontend

十年热恋 提交于 2019-12-13 00:48:22

问题


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


来源:https://stackoverflow.com/questions/29888430/yii2-redirect-to-backend-after-user-registration-from-frontend

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!