How to create a page using another view?

Hi,

As of today, i am 2 days old to Yii so any help is appreciated.

I have a tbl_subscriber in mysql to collect newsletter subscribers (id, name, email). I have gone through Model & CRUD generator in Gii.

I notice that going to /subscriber/create/ i can see the form. However, if i would like to keep this form for admin module and use this form on another page, e.g. /subscriber/signup/

I have gone through the doc, but still couldnt understand how to do it. Does any1 has any idea how this can be achieved?

Thanks,

11th

So you want the users to be able to subscribe to a newsletter or something (by creating an account)?

My suggestion would be to create a new action in your SiteController called actionSubscribe instead of using the create function of the SubscriberController. After that you put the "create" and "_form" views that gii built from views/subscriber/ to /views/site so that your SiteController that has this new action is able to find the views there.

for the logic in actionSubscribe():




public function actionSubscribe()

	{

		$model=new Subscriber;


		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);


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

		{

			$model->attributes=$_POST['Subscriber'];

			if($model->save())

				$this->redirect(array('success',)); // Normally this would lead you to the detail view, but you should render a success page or you could display a flash-message

		}


		$this->render('create',array(

			'model'=>$model,

		));

	}

Don’t forget to put a view file called success.php to views/site/ so you won’t get an error with the above example.

Now when typing /site/subscribe you would have your form displayed and everything is working as before, just in another controller

This is just a rough suggestion but should do the trick

EDIT: If you want your url to be /subscriber/signup you just would have to alter the actionCreate in your SubscriberController to actionSignUp. But don’t forget to add “signup” to the rules() function (changing create to signup)

greetings,

Haensel

Thanks Haensel for the quick response!!!

I ended up doing something similar. I created an action, actionSignup in SubscriberController by copying the actionCreate function.


public function actionSignup()

	{

		$model=new Subscriber;


		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);


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

		{

			$model->attributes=$_POST['Subscriber'];

			if($model->save())

				$this->redirect(array('site/page', 'view'=>'subscriber.thankyou'));

		}


		$this->render('signup',array(

			'model'=>$model,

		));

	}

Because i didnt want to touch the subscriber/create.php view generated by Gii, i duplicated it (signup.php) and change its content (and different layout) to suit the front end website.

i then redirected them to a static thankyou page.

Now i will look into using flash message so i do not need to create an extra page.

Thanks alot!!

11th

It works either way, just a matter of taste. Your way is perfectly fine :)

If you need some help regarding flash messages: http://www.yiiframework.com/wiki/21/how-to-work-with-flash-messages

Glad I could help