[SOLVED]add new controller manually

hi, by default, my app was started using the almighty gii…

now I wish to add some new pages without using gii,

I created a controller e.g CompaniesController. it has a basic accessRules() and an actionIndex. now my question is, let’s say I wanna add an extra link at the ‘main.php’ of the layouts called ‘Companies’, how will I link that to my index.php dynamically?

by default, yii generated code has this things




	<div id="mainmenu">

		<?php $this->widget('zii.widgets.CMenu',array(

			'items'=>array(

				array('label'=>'Home', 'url'=>array('/site/index')),

				array('label'=>'About', 'url'=>array('/site/page', 'view'=>'about')),

				array('label'=>'Contact', 'url'=>array('/site/contact')),



and then previously, I added this link




array('label'=>'My Companies', 'url'=>array('/wsmembersdetails/index'),'visible'=>!Yii::app()->user->isGuest),



that thing above shows a url array made up of a model and the actionIndex correct ?

so how will I add a new link if it doesn’t really have a model ?

ok i solved it like this




array('label'=>'Companies', 'url'=>array('/companies/index'),'visible'=>Yii::app()->user->isGuest),



it should only appear if the person is not logged in

my new problem now is, the data doesn’t appear at the index view

here’s my new controller




<?php


class CompaniesController extends Controller

{

	public $layout = '//layouts/column2'; 

	

	public function accessRules()

	{

		return array(

			array(

					'allow',

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

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

			),

			

		);

	}

	

	public function actionIndex()

	{

		$dataProvider = new CActiveDataProvider('Wsmembersdetails');

		$this->render('index',array('dataProvider'=>$dataProvider));

	}

}



here’s my index.php





<?php

$this->breadcrumbs=array(

	'Company Listings',

);


?>


<h1>Companies</h1>


<?php $this->widget('zii.widgets.CListView', array(

	'dataProvider'=>$dataProvider,

	'itemView'=>'_view',

)); ?>



and here’s my _view file code




<div class="view">

	<b><?php echo CHtml::encode($data->getAttributeLabel('CompanyName')); ?>:</b>

	<?php echo CHtml::link(CHtml::encode($data->CompanyName), array('view', 'id'=>$data->CompanyID)); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('ContactName')); ?>:</b>

	<?php echo CHtml::encode($data->ContactName); ?>

	<br />

</div>



when I click the Companies link at the home page menu, i get redirected to

‘index.php?r=companies/index’

but then the page says ‘Error 404’ you are not authorized…

why is that happening?, I indicated it at the controller’s accessrules that everyone

can view the ‘index’ correct? or did i missed something?

Did you add filters?




	/**

	 * @return array action filters

	 */

	public function filters()

	{

		return array(

			'accessControl', // perform access control for CRUD operations

		);

	}



Without this rules will not be taken in account.

And are you sure that the error is generated by accesscontrol? Try to change the index into:




        public function actionIndex()

        {

                echo 'works';

        }




If you will see the echo, it means that there is something else that raises the exception, maybe you have some securuty check in the models.

I added the filters and changed the index




class CompaniesController extends Controller

{

	public $layout = '//layouts/column2'; 

	

	public function filters()

	{

		return array(

			'accessControl',

		);

	}

	

	public function accessRules()

	{

		return array(

			array(

					'allow',

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

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

			),

			

		);

	}

	

	public function actionIndex()

	{

//		$dataProvider = new CActiveDataProvider('Wsmembersdetails');

//		$this->render('index',array('dataProvider'=>$dataProvider));

		echo "works";

	}

}




and I was able to see the word "works" but, the page turned white

Of corse it turned white, in your controller you simply wrote ‘works’, and it worked successfully.

It means that the error is not in the rules, but somewere else in your code.

Try to find what is the line that raise the exception, maybe there are some good reason for it.

I found what causes the error, it’s this snippet under my model





	public function afterFind()

	{

		if($this->MemberShipID != Yii::app()->user->id)

			throw new CHttpException('404','You are not authorized');

	}




i don’t really remember what’s the use of this function, but as far as I know

i placed it there to only show the ‘own stuff’ of the particular user that is currently logged in, but I tried to comment it and my app seem working and I don’t see the stuffs of other users while being logged in using a dummy account.