The directory structure of Yii's default makes learning curve deeper

The default directory structure of Yii Application is as follows:

controllers/actions: protected/controllers/$ControllerID.php;

models: protected/models/$modelClassID.php;

views: views/$controllerID/$actionID.php.

It seems this directory organization clearly splits the MVC parts. But in fact they are tightly coupled in logic: controllers/actions refers to views and models, views incorporates model class attributes. Everytime navigating the code logic to invoke models/views, you have to guess the file locations to open the files.

I think taking the following style can make it simpler, when you are learning/working on Yii:

controllers: 'protected/'.$controllerID.'/'.$controllerID.'.php'

actions: 'protected/'.$controllerID.'/'.$actionID.'.php'

models: 'protected/'.$controllerID.'/'.$modelClassID.'Model.php'

views: 'protected/'.$controllerID.'/'.$actionID.'View.php'

common/shared: 'protected/models', 'protected/layouts', 'protected/views'

components: same as controllers

For example, a controller named 'site' with action 'login', model 'LoginForm' and view are as follows:





The above naming conventions can easily be linked to the requesting URL: '?r=site/login'. And you will never be lost in the editor with same name files (Does the tab name 'login.php' refer to the action or the view?). I know Yii directory can be dynamically configured. But the filenames are not. So I strongly want to hear the voices from Qiang and you people. Thanks!

I think there should be no problem for model and action file locations because Yii doesn't really enforce them to be in specific directories. The only rule that Yii enforces on file names is that the file name is the same as class name. So as long as you name your classes well, you won't confuse yourself with file names.

The main complaint seems to be in the view file organization. In fact, that is not a problem either. If you want to have your view files staying together with controller class files, you can extend from CExtController instead of CController. By doing so, your view files will be located under the "views" subdirectory of the directory containing the class file.

I will definitely implement things you have described here for my project. Controllers/Views should always be behind each other.

Qiang, big thanks for your fast reply. I wonder if we can use the yiic tool to automatically generate the directory structure as I mentioned above. It seems I have to create a new cli command and new templates, or modify the framework cli part directly. And most important, the controller file names are hardcoded in the framework with a 'Controller' suffix and all view files are in fact hardcoded to one location in the function CController::getViewPath.

I'm not experienced in php, except C/C++. My simple intention to learn Yii is to get a quick & easy tool to create my own web site (before started, I skimmed some guides of Prado and Symfony). The reason why I said as the above topic is because I found out that it's not easy to start & focus my work directly on MVCs, without specific knowledge of their invocation relationship (The official guide of Yii is very concise and lacking a clear calling route diagram). So I spent extra several days digging into the Yii framework to figure out the relationship between those files. Then I found I was jumpping among some directories and the target file names are not so plain. The whole framework is complex, sophisticated, and awesome showing everywhere designer's high level skills, but really not easy for end users to understand and control.

I mentioned this issue standing at the point of fresher to learn Yii. The current directory organization of Yii Application is not a thing for me now.


I will definitely implement things you have described here for my project. Controllers/Views should always be behind each other.

Waylex, I thank first if you can share your experience later. I don't have much confidence since some things were built in the framework due to the designer's default perspective: use directories to separate MVC, instead of file/class names.

Thank you for your comments!

Yes, you definitely can create new yiic commands to generate webapps (and perform other tasks) that fit your style. Just drop in the command class files under "protected/commands" and you are ready to go.

Regarding the view file organization, there are several more reasons for the current organization. For example, the current organization works better with theming feature. And some other widely adopted frameworks (e.g. CakePHP) also use the same organization. The current organization may also facilitate designers since they don't need to jump around in the file tree.

The naming for controller class files is to following the general class file naming convention.

I will add some sequence diagram to the MVC part of guide. Thanks!


The official guide of Yii is very concise and lacking a clear calling route diagram

Well said. I'm still trying to figure out the big picture of an application flow including application, controller, widgets, layouts, themes, filters and actions.

We discussed this a bit at http://www.yiiframew…opic,66.0.html. The gist of it ended up that controllers where changed to allow controllers be placed in folders. But if you really wanted to you could use your own base controller to do lookups where ever you want.


I'd like to offer my project structure. Here it is:

/producing/ - Console application to manage project.


/protected/application/ - Controllers and their views

/protected/extensions/ - Components, widgets and YII overrides

/protected/framework/ - Yii itself

/protected/logging - Log files

/protected/runtime - Runtime files

/protected/session - Session files

/resources/ - Published assets


/websites/Admin - Back-end

/websites/Guest - Front-end

Comments, complaints?

qiang, the current configuration is great.  It's much like a lot other frameworks, and it's well designed.  Great work.

waylex: Yii allows you to use your proposed directory structure.

jonah: thank you for your support. Yes, we will stick to the current default directory structure.


waylex: Yii allows you to use your proposed directory structure.

Thanks, I have already configured Yii this way and all seems to work fine. The only thing that's left is action-enabled widgets, and all pieces will come together!

Thanks for your support!

I nearly succeeded customizing yiic to create the directory organization as I mentioned above, except one thing: the __autoload() magic method failed when loading the LoginFormModel which I put it under the controller dir. I guess this magic method is called by "new LoginFormModel" and the controller path was not included by default app setting. The reason why I can not pre-include the model is because the controller name is not fixed one, e.g. 'application/site/site.php' invokes 'application/site/LoginFormModel.php'.

I really don’t see the point in changing the directory structure.

Yii’s structure is very much in line with what other frameworks are doing. It makes perfect sense to me.

But since Yii uses autoloading it shouldn’t be a problem at all to organize the directories in any way you may prefer

I think the mvc part is good similiar to other frameworks… only the extensions vs components is confusing… we just put too much in components…





controller (front controller)


I personally don’t find hard to understand current directory structure, I think is clear and really easy to maintain. Even though I kind of like the idea of CExtController as explained by Qiang for views, I think the folder structure is great and should’t be touch and I agree with MarcS about you changing the way it is, I believe that with the use of CExtController and a bit of ‘namespace’ tweaking, you dont even need to change its autoload or using After all, Yii is very well known for its flexible nature to fit everyone needs isn’t it?

I actually think this makes things more difficult to follow and starts to more tightly couple parts of the MVC together. Models don’t belong with the controller, many controllers should have access to the same class. I see no benefit to splitting the actions into their own files, the controller and it’s actions are encapsulated in their own class.

On top of that you’re making it harder for other Yii developers who might work on your project in the long term. Given that you’ve stated your lack of experience of PHP and, I’m guessing, web based MVC frameworks as a whole I would take the time to learn default behaviour before you start restructuring the framework. I can’t help but feel that you’re trying to exert your C/C++ expectations onto something you’re not familiar with and, IMO, that’s not a good thing.