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

The standard site controller renders actionIndex with


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


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:

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',			



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


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


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


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!


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.