php Yii框架登录后重定向页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8410438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Redirect page after login in Yii framework
提问by NewUser
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.
我是 Yii 框架的新手。在 Yii 中,当您默认登录时,它会重定向到索引页面。我希望当我登录到 Yii 时,页面将重定向到另一个页面而不是索引页面。所以任何人都可以帮助我。任何帮助或建议将是非常可观的。
[edit]
[编辑]
how the redirect will work when I will use user module as after login the page is redirected towards profile page?
当我将使用用户模块时重定向将如何工作,因为登录后页面被重定向到个人资料页面?
回答by Jon
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:
您可以(实际上,如果要发生任何重定向,则必须)指定要重定向到控制器actionLogin
方法内部的 URL 。登录成功后,你会看到类似这样的代码:
$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.
顺便说一句, usingYii::app()->user->returnUrl
使重定向页面能够在用户被重定向到登录页面之前将用户返回到他们打算访问的 URL。
回答by dhimes
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
要将用户重定向到登录后的页面,请在 gii 中为您的用户登录后将被定向到的页面创建一个新控制器。我将在此处称该控制器为“应用程序”。Gii 会自动为你创建一些文件——一个是 /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
在 AppController.php 中,您将拥有一个名为 actionIndex 的默认公共函数(方法)。这个默认方法的目的是调用(渲染)/protected/views/app/index.php 文件(也是由gii 为你创建的)。index.php 是您的用户登录后将看到的文件。这是您要修改以构建应用程序的文件。返回 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 站点上的讨论中 的帖子)
回答by Thu Ra
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'),
........
),
),
回答by Waseem shah
$this->redirect($this->createUrl('yourcontroller/youraction'));