How does the site controller know to look in the site folder for the 'index' view

The standard site controller renders actionIndex with


$this->render('index');

How does it know to look in the site folder to find index?

I ask because when I run a unit test on a controller action, this fails. I have to modify it to


$this->render('/site/index');

I’ve tried all sorts of includes in the tests, but haven’t managed to make them work without modifying the controller. How can I make the test work without having to modify the controller?

See here for information, where Yii looks for view files:

http://www.yiiframework.com/doc/guide/basics.view

Why do you call controller actions in unit tests? I think, it’s better to use functional tests (selenium) to test controller actions.

Unfortunately, that does not seem to work when running a test.

I’m fairly new to test driven development. I was going to create a simple test for each action. Just to check for a piece of text on each page. Then use functional tests for more in depth testing. I was going to do this because unit tests run quicker, and I didn’t think it would take very long to write them. However this problem combined with another (I can’t work out how to access the rendered page from the test) has led me to abandon this. I’ll just write functional test as you suggest.

I also am having a bit of trouble with the view paths.

What I am trying to accomplish is when having someone type index.php/admin/, it would send them to

  • controllers/admin/<controller>

  • views/admin/<view>

I tried with the following rules:




				'admin' => 'admin/index',			

				'admin/<controller:\w+>'=>'admin/<controller>',	

				'admin/<controller:\w+>/<action:\w+>'=>'admin/<controller>/<action>',	



My problem is that in my IndexController.php I have to do this:




$this->render('../index');



I found out the reason was because my viewPath resolves to this when I call getViewPath:

protected/views/admin/index

If I create a folder called "index" in my admin folder and put the index.php inside, it works when calling:




$this->render('index');



I don’t know why it appends the index as a folder instead of having protected/views/admin/ as the view path.

I am also using themes mind you.

Any help in why this is happening would be great!

Thanks.

Hi Kheang

I don’t know enough to help you. You might want to post your problem in a separate thread. More people will be likely to see it that way.

I suppose you created a controller instance and then called the action. That’s why it doesn’t really work: The framework does much more before it calls a controller action: Init the application, use an URL manager to inspect $_GET to find the controller and action, etc. etc.

You’d have to emulate all request parameters, create an application and call run() to really create the same conditions. Much more cumbersome than using functional tests right away.