简体   繁体   English

在Yii框架中登录后重定向页面

[英]Redirect page after login in Yii framework

I am newbie to the Yii Framework. 我是Yii框架的新手。 In Yii when you login by default it redirects to the index page. 在Yii中,当您默认登录时,它会重定向到索引页面。 I want that when I will login to Yii the page will redirect to another page not the index page. 我希望当我登录Yii时,页面将重定向到另一个页面,而不是索引页面。 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? 当我使用用户模块时,重定向将如何工作,因为登录后页面被重定向到个人资料页面?

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. 您可以(实际上,如果要进行任何重定向,必须)指定要重定向到控制器的actionLogin方法内的URL。 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. 将此更改为CController::redirect方法支持的任何参数,您可以控制用户在登录后重定向的位置。

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. 另外,使用Yii::app()->user->returnUrl可以使重定向页面在重定向到登录页面之前将用户返回到他们打算访问的URL。

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中为用户登录后指向的页面创建一个新控制器。我将在此处调用此控制器“app”。 Gii will automagically create some files for you- one will be /protected/models/AppController.php Gii会自动为你创建一些文件 - 一个是/protected/models/AppController.php

In AppController.php, you will have a default public function (method) called actionIndex. 在AppController.php中,您将拥有一个名为actionIndex的默认公共函数(方法)。 The purpose of this default method is to call (render) the /protected/views/app/index.php file (also created by gii for you). 此默认方法的目的是调用(呈现)/protected/views/app/index.php文件(也由gii为您创建)。 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. index.php是用户登录后将看到的文件。这是您要修改以构建应用程序的文件。 Go back to SiteController.php and change the argument of redirect() in the actionLogin() method 返回SiteController.php并在actionLogin()方法中更改redirect()的参数

 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 ) (这基本上是关于yiiframework网站讨论的帖子

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'),
                ........
    ),
),
$this->redirect($this->createUrl('yourcontroller/youraction'));

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

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