help with reusable code

I have two modules (group and pages) which have a news section. The model is the same but with some changes in views and controllers. My structure is as follows

/ modules / groups / controllers / NewsController.php

/ modules / pages / controllers / NewsController.php

/ modules / groups / models / NewsGroup.php (extend models/News.php)

/ modules / pages / models / NewsPage.php (extend models/News.php)

I Have the views, behaviors and widgets in:

/ components / news /

and reuse them in the two modules.




/modules/groups/NewsController.php

...


public function actionCreate()

	{

		

		if (!Yii::app()->user->checkAccess("groups.createNews"))

			$this->accessDenied();

			

		$news=new NewsGroup();

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

		{

			$news->attributes=$_POST['NewsGroup'];

			if($this->group->addNews($news))

				$this->redirect($news->getUrl($this->group));

		}


		$this->render('application.components.news.views.create',array(

			'model'=>$news,

                        //Specific view block for groups /modules/groups/views/news/_permissions.php

			'permissions_view'=>$this->renderPartial('_permissions', array('model'=>$news), true)

		));

		

	}

...






/modules/pages/NewsController.php

...


public function actionCreate()

	{

		

		if (!Yii::app()->user->checkAccess("pages.createNews"))

			$this->accessDenied();

			

		$news=new NewsPage();

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

		{

			$news->attributes=$_POST['NewsPage'];

			if($this->page->addNews($news))

				$this->redirect($news->getUrl($this->page));

		}


		$this->render('application.components.news.views.create',array(

			'model'=>$news,

                        //Specific view block for pages /modules/pages/views/news/_permissions.php

			'permissions_view'=>$this->renderPartial('_permissions', array('model'=>$news), true)

		));

		

	}

...



Is this the right way?, Is it better to repeat the code in views?

or How do you implement a news section in two different modules?

or Do you know any example code of how to implement a news(or another) section in two different modules?

Thanks.

Why not create a widget for this purpose?

Could you show me a code example about how do it?

Thanks

You should check this url

http://www.yiiframework.com/doc/guide/1.1/en/basics.view#widget

Now I have the following:

/ modules / groups / controllers / NewsController.php

/ modules / pages / controllers / NewsController.php




/modules/groups/NewsController.php

...

public function actionCreate()

	{

		

		if (!Yii::app()->user->checkAccess("groups.createNews"))

			$this->accessDenied();

			

		$news=new NewsGroup();

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

		{

			$news->attributes=$_POST['NewsGroup'];

			if($this->group->addNews($news))

				$this->redirect($news->getUrl($this->group));

		}


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

			'model'=>$news,

		));

		

	}

...






/modules/groups/views/create.php


<h1><?php echo Yii::t('groups','Create news')?></h1>


<?php $this->widget('application.components.pack.news.CCreateFormNewsWidget', array(

	'model'=>$model)

);?>







/components/pack/news/CCreateFormNewsWidget.php


class CCreateFormNewsWidget extends CWidget{

	

	public $model;

	

	public function run(){

		

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

	}

	

}






/components/pack/news/views/_form.php


<?php $form=$this->beginWidget('CActiveForm', array(

		'htmlOptions'=>array('enctype'=>'multipart/form-data'),

));?>


     <div class="row">

        <?php echo $form->labelEx($model,'title'); ?>

        <?php echo $form->textField($model,'title',array('size'=>50,'maxlength'=>150)); ?>

        <?php echo $form->error($model,'title'); ?>

    </div>

    

    <div class="row">

        <?php echo $form->labelEx($model,'entry'); ?>

        <?php echo $form->textField($model,'entry', array('size'=>50,'maxlength'=>150)); ?>

        <?php echo $form->error($model,'entry'); ?>

    </div>

    

    <?php if ($this->controller->module->id == 'groups'):?>

     <div class="row">

     	<?php echo $form->labelEx($model,'access_flag'); ?>

		<?php echo $form->checkBox($model,'access_flag'); ?> <?php  echo Yii::t('groups','Only members.')?>

	</div>

	<?php endif;?>

    

    <div class="row buttons">

		<?php echo CHtml::submitButton($model->isNewRecord ? Yii::t('news','Create') : Yii::t('news','Save')); ?>

	</div>

    

<?php $this->endWidget(); ?>



Now i can reuse CCreateFormNewsWidget in other views.

is it correct? Does it follow the best practices?

Thanks.

I don’t think that It is bad one… :P