Changes
Title
unchanged
Alternative folder structure for a standard Yii app
Category
unchanged
How-tos
Yii version
unchanged
Tags
unchanged
code organisation, protected folder structure
Content
changed
[...]
### `components` folder
In default Yii application, virtually _anything that looks similar to the component_ is kept all together in the `protected/components` folder. I found this less useful, so I proposed to separate this folder into four subfolders:
- `behaviours
` -- contains all the behaviours,
-
_`extenders
_` -- all classes, that extends framework's base classes, for example `Controller`,
-
_`functions
_` -- classes, that acts like common function repositories
or helpers,
-
_`widgets
_` -- for all the visual widgets.
**Note: Some propose naming `functions` folder with better suiting `helpers` name.**
Changing structure this way, you have to also change small part of your app's configuration. I.e. change:[...]
Similar change goes for models. Default Yii code organisation keeps all models together and distinguish form models from data models by adding `Form` into form models class name. That was something, that I didn't like from the very beginning, so I split `protected/models` folder structure into two subfolders:
- _`data
_` -- all data related models`,
-
_`form
_` -- models for all forms.
Similar change to application's configuration must be taken here. Change:[...]
There were some forum rummors a year or two ago, that putting layouts in the same structure level as all other controller-related views might be sometimes confusing. I fully agree with that. Situation gets even worse, if you're using widgets, that renders some views. So, to cleanup `protected/views` folder structure, I've separated it into three subfolders:
- _`controllers
_` -- moved all other views' folder here,
-
_`layouts
_` -- remains untouched, as in original, auto-generated app,
-
_`widgets
_` -- views for possible widgets.
Views that are general or application-wide (for example `error.php`) are kept in original place, i.e. directly under `protected/views` folder, without further spearation to some folder. But, you may consider creating `general` subfolder and keep them there.
To make your application working with new `views` folder strucuture, this time you don't change nothing in app's config. Instead, you have to modify your base controller (`protected/components/Controller.php` in original Yii structure or `protected/components/extenders/Controller.php` in above presented), i.e. the one from which all your app's controllers extends from.[...]