How to reuse the code?

Hi,

I have a contact form that I want to display it in different sections of my website, but I don’t know how to reuse same contact form (model, view, controller).

I defined 3 models: section1Model, section2Model, contactFormModel and 3 controllers (secion1Controller, section2Controller, contactFormController).

So, I have the following links: /section1/index and /section2/index and i want to add the contact form at the bottom of each of this pages.

First time I was thinking i can use a widget but the widget has no actions.

Please help me to solve my problem.

Thank you.

Create a widget.

A widget is a small controller that performs a single task.

Create a class in componentes that extends Cwidget, then:




public class myWdiget extends CWidget

{

   public function run()

   {

        ...

        $this->render('viewName');

   }

}




The views must be located in components/view.

You can include this widget in your views with:




<?php $this->widget('myWidget')?>



Thank you Zaccaria! Based on your suggestion I made a widget like this:




class ContactFormWidget extends CWidget {

    public function run() {

        $scenario = $this->getController()->id;

        $model = new ContactForm($scenario);

        if(isset($_POST['ContactForm'])) {

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

		    if($model->validate())

		    {

				/*

					php code for actions

					.......

				*/

				Yii::app()->user->setFlash('contact','Thank you for contacting us. We will respond to you as soon as possible.');

				$this->getController()->refresh();

	        }

		}

        $this->render('contactFormWidget',array('model'=>$model));

    }

}



what do you think about my approach?

Can you tell me how to add a captcha?