How To Continue Last Action After Login?

[font="Arial"][size="3"]Hi,

I have a page that contains a form.

I want to allow all users to view this form, but on submission of the form, if the user is not logged in - I want to redirect him to the login page. After the user logs in, I want to continue the original form submission.

For example in this page :http://www.yiiframew…dfasdf#comments When the user clicks on "submit" he will be redirected to the login page and after login his comment will be submitted has a logged in user.

How should I do that?

Thanks![/size][/font]

There are several pages on this subject (look for ‘returnurl’, ‘setreturnurl’). Example:

http://www.yiiframework.com/wiki/43/adding-login-form-to-each-page/

Thanks, but this solution just get me back to the last page.

After the login I need to save the information the user [size=2]originality[/size][size=2] submitted (before he was redirected to login) or to resubmit his form[/size]

This user found a solution that may work for you:

http://www.yiiframework.com/forum/index.php/topic/4798-remember-originally-submitted-form-on-redirect-for-required-login/

Dear Friend

Let us consider a Model Comment.

1.Now we make all the users to visualize the form.

CommentController.php




public function accessRules()

	{

		return array(

			array('allow',  // allow all users to perform 'index','view' and 'create' actions

				'actions'=>array('index','view','create'),

				'users'=>array('*'),

			),

.....other rules to follow....



  1. In _form.php, let us have two submitting urls.

If user is guest, we are directing the user to site/login if he clicks the submit button.

Additionally we are also submitting the class name of the model along with form data.

If user is authenticated, form submission occurs normally.




<!--other form elements-->


<div class="row buttons">

	<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save',array(

		'submit'=>(Yii::app()->user->isGuest)?array("site/login"):"",

		'params'=>array("class"=>get_class($model)),

		

		)); ?>

	</div>



In action login we are gathering the form data and storing it in session in the name of the class.

We also setting the return url to comment/create.

By storing it in class name, the following code also works for forms of other models also.

SiteController.php




public function actionLogin()

{

	$model=new LoginForm;

       

        //Now add these lines

		

	if(isset($_POST['class']))

	{	

		Yii::app()->user->setState($_POST['class'],$_POST[$_POST['class']]);

		Yii::app()->user->returnUrl=Yii::app()->createUrl(strtolower($_POST['class']."/create"));

	}


............................................................................



In controller we can get the values from session, if it exists.




public function actionCreate()

{

	$model=new Comment;	

	if(Yii::app()->user->hasState("Comment"))

	{	

		$model->attributes=Yii::app()->user->getState("Comment");

		Yii::app()->user->setState('Comment',null);

	}


.............................................................................



I hope this will help you.

Regards.