Redirect page after login in Yii framework

2020-03-17 04:37发布

I am newbie to the Yii Framework. In Yii when you login by default it redirects to the index page. I want that when I will login to Yii the page will redirect to another page not the index page. So can anyone help me in this. Any help or suggestions will be highly appreciable.

[edit]

how the redirect will work when I will use user module as after login the page is redirected towards profile page?

标签: php yii
4条回答
Rolldiameter
2楼-- · 2020-03-17 05:01

you can redirect to site/index after logged in using user module.

'modules'=>array(
    // user extension
    'user'=>array(
               ...........
        # page after login
         //'returnUrl' => array('/user/profile'),
        'returnUrl' => array('/site/index'),
                ........
    ),
),
查看更多
该账号已被封号
3楼-- · 2020-03-17 05:14
$this->redirect($this->createUrl('yourcontroller/youraction'));
查看更多
男人必须洒脱
4楼-- · 2020-03-17 05:25

To redirect the user to a page after login, create a new controller in gii for the page your user will be directed to after s/he logs in. I'll call this controller 'app' here. Gii will automagically create some files for you- one will be /protected/models/AppController.php

In AppController.php, you will have a default public function (method) called actionIndex. The purpose of this default method is to call (render) the /protected/views/app/index.php file (also created by gii for you). index.php is the file your users will see once they log in. That is the file you will want to modify to build your app. Go back to SiteController.php and change the argument of redirect() in the actionLogin() method

 if(isset($_POST['LoginForm']))
            {
                    $model->attributes=$_POST['LoginForm'];
                    // validate user input and redirect to the previous page if valid
                    if($model->validate() && $model->login())
                    // since my controller is /protected/controllers/AppController.php
                            $this->redirect(array('app/index'));
            }

This should get you started. (This is essentially my post on the discussion at the yiiframework site)

查看更多
Deceive 欺骗
5楼-- · 2020-03-17 05:27

You can (and indeed, must, if any redirection is going to take place) specify the URL to redirect to inside your controller's actionLogin method. After a successful login, you will see something like this code:

$this->redirect(Yii::app()->user->returnUrl);

Change this to any parameter that the CController::redirect method supports, and you can control where the user is redirected after login.

As an aside, using Yii::app()->user->returnUrl enables the redirect page to return the user back to the URL they intended to visit before being redirected to the login page.

查看更多
登录 后发表回答