Boost PC performance: How more available memory can improve productivity
Organize directories for applications with front-end and back-end with yii - CuongNV
1. ORGANIZE DIRECTORIES FOR APPLICATIONS
WITH FRONT-END AND BACK-END WITH YII
Author : Nguyen Van Cuong
2. INTRODUCTION
Large applications are often divided into front-end
and back-end (or even more ends) depending on
the target user groups
The front-end should be used by common users,
while the back-end mainly the administrators or
staff members
3. Example :
Need build a custom school system,
This have a front end and back end.
front end:
teachers,
students
parents
- administrator who can manage all, create schedule etc etc
- form builder (drag and drop).
back end:
Super administrator
- Customize fields (change fields name)
- Customize category
4. HOW ?
Have many another way to organize directories .
One way to organize directories using
WebApplicationEnd behavior.
5.
6. Front-end SiteController and all front-end views are
files generated by yiic tool. You have to create
back-end SiteController and back-end views by
yourself
Create different config files for both ends. Since
these files usually have much in common, we will
"inherit" them from the main.php config:
7. STEP 1
webroot/protected/config/admin.php :
return CMap::mergeArray(
require(dirname(__FILE__).'/main.php'),
array(
// Put front-end settings there
// (for example, url rules).
)
);
webroot/protected/config/home.php :
we config similar…
8. By default, Yii will try to find controllers and views in
protected/controllers and protected/views
directories respectively. We have to change this
behavior and force Yii to search controllers and
views in the "back" or "front" subdirectories
depending on the currently running end.
Create a file "WebApplicationEndBehavior.php"
with the following contents:
STEP 2
9. class WebApplicationEndBehavior extends CBehavior {
private $_endName;
public function getEndName() {
return $this->_endName;
}
public function runEnd($name) {
$this->_endName = $name;
$this->onModuleCreate = array($this, 'changeModulePaths');
$this->onModuleCreate(new CEvent($this->owner)); $this->owner-
>run();
}
10. public function onModuleCreate($event) {
$this->raiseEvent('onModuleCreate', $event);
}
protected function changeModulePaths($event) {
$event->sender->controllerPath .=
DIRECTORY_SEPARATOR.$this->_endName;
$event->sender->viewPath .=
DIRECTORY_SEPARATOR.$this->_endName;
}
11. Now add some lines to the main config file:
'behaviors'=>array(
'runEnd'=>array(
'class'=>'application.components.WebApplicationE
ndBehavior',
),
),
Now our application has a new method runEnd (to run
one of the application's ends) and a new event
onModuleCreate. By raising this event from a web
module we can change modules' properties. Controllers
and views paths are changed in the attached handler
"changeModulePaths".
12. STEP 3
Finally, let's protect back-end by creating a parent
controller for all back-end controllers
webroot/protected/controllers/back/SiteController.ph
p must extend this controller to perform access
checking.
After all , create new index.php and backend.php
files are:
13. BENEFITS
The advantages of this Organize directories is :
controller and the view of the two part - and page
admin server - is totally independent of each other,
but they use the same model folder. This is useful
because the actual website your homepage and the
management will take data from one database to
only. And more, when generate code using the
code gii still be included in the corresponding !