Yii directory layout and the Controller class

I am just getting acquainted with Yii and am a little confused about the concept of components and particularly components/Controller.php. First off, is there a file at components/Controller.php in all Yii applications? Is this a convention? From a design standpoint, I don’t quite understand why that Controller class isn’t in the controllers directory. I’m not sure what makes this Controller class a component, while the other controller classes that extend it are in the controllers directory.



Hey Ryan

controller folder contains only the controllers, classes that extend core funcionality usually goes in the components folder .This is a convention, but you can change its location if you want

The point to exist it is that all your controllers extends from it so it will be easier to add funcionalities or change some property to all controllers, like all of your controllers uses the same layout, so to you just change "Controller" file


I think you could move the Controller class into the controllers folder and things would still work (untested).

If you look in your config/main.php typically you are importing the contents of the components folder.

I am working on an application that has a front end and back end. In my case the Controller class that is referenced by each controller is in the admin/components. This way I can avoid some duplication.

It’s also not a bad idea to extend the controller within the components folder with a new class MyAdminController extends Controller and MyPublicController extends Controller. This way each public controller (frontend) extends MyPublicController etc.

It’s very flexible, out of the box this is just the default file arrangement.

// autoloading model and component classes this is in admin config






// the next stuff is in a different file

// in the front end

// define an alias


list($mainpath, $scrap)= explode('protected', $path);



*  other stuff here 


// autoloading model and component classes





Wow, Gustavo got in there while I was replying :lol:

Hope this is a small clue on your way to Yii mastery.

welcome aboard


Doodle explained better what I had to say

That is a good practic that I use and recommend too

Hope this all can help you