Yii Framework Forum: The directory structure of Yii's default makes learning curve deeper - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

The directory structure of Yii's default makes learning curve deeper Rate Topic: -----

Poll: Do you agree with my opinion that current directory organization of a Yii Application should be changed for easier understanding/operation? (52 member(s) have cast votes)

Do you agree with my opinion that current directory organization of a Yii Application should be changed for easier understanding/operation?

  1. Yes, it's like a maze when I'm trying to figure/work out one controller/action with its model and its view. (14 votes [26.92%])

    Percentage of vote: 26.92%

  2. No, current directory structure is fine for me. (38 votes [73.08%])

    Percentage of vote: 73.08%

Vote

#1 User is offline   celleo 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 15-December 08

Posted 21 December 2008 - 11:02 AM

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:
protected/site/site.php
protected/site/login.php
protected/site/LoginFormModel.php
protected/site/loginView.php


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!
0

#2 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,900
  • Joined: 04-October 08
  • Location:DC, USA

Posted 21 December 2008 - 12:24 PM

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.
0

#3 User is offline   sergeymorkovkin 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 99
  • Joined: 15-December 08

Posted 21 December 2008 - 02:46 PM

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

#4 User is offline   celleo 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 15-December 08

Posted 22 December 2008 - 03:15 AM

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.
0

#5 User is offline   celleo 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 15-December 08

Posted 22 December 2008 - 03:22 AM

Quote

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.
0

#6 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,900
  • Joined: 04-October 08
  • Location:DC, USA

Posted 22 December 2008 - 07:20 AM

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!
0

#7 User is offline   sergeymorkovkin 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 99
  • Joined: 15-December 08

Posted 22 December 2008 - 09:01 AM

Quote

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.
0

#8 User is offline   notzippy 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 201
  • Joined: 06-October 08

Posted 22 December 2008 - 10:24 AM

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.

nz
0

#9 User is offline   sergeymorkovkin 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 99
  • Joined: 15-December 08

Posted 22 December 2008 - 04:56 PM

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


/producing/ - Console application to manage project.
/protected/
/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/
/websites/Admin - Back-end
/websites/Guest - Front-end


Comments, complaints?
0

#10 User is offline   jonah 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 733
  • Joined: 27-November 08
  • Location:California (US)

Posted 22 December 2008 - 11:53 PM

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

#11 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,900
  • Joined: 04-October 08
  • Location:DC, USA

Posted 23 December 2008 - 07:09 AM

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.
0

#12 User is offline   sergeymorkovkin 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 99
  • Joined: 15-December 08

Posted 23 December 2008 - 08:15 AM

Quote

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!
0

#13 User is offline   celleo 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 15-December 08

Posted 23 December 2008 - 11:30 AM

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'.
0

#14 User is offline   MarcS 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 97
  • Joined: 05-July 09

Posted 12 August 2009 - 10:35 AM

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
0

#15 User is offline   mech7 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 220
  • Joined: 26-March 09

Posted 11 August 2010 - 01:25 AM

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..

widgets
portlets
components
helpers
controller (front controller)
useridentity
0

#16 User is offline   Antonio Ramirez 

  • Elite Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 1,450
  • Joined: 04-October 10

Posted 09 January 2011 - 07:59 PM

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 http://www.yiiframew...oloader-detail. After all, Yii is very well known for its flexible nature to fit everyone needs isn't it?
┬┐How long would it take for you to understand that you own nothing in this world?

www.ramirezcobos.com
www.2amigos.us
www.github.com/tonydspaniard
www.github.com/2amigos


Posted Image
0

#17 User is offline   Say_Ten 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 109
  • Joined: 17-September 10

Posted 28 July 2011 - 08:25 AM

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.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users