ORGANIZE DIRECTORIES FOR APPLICATIONS
WITH FRONT-END AND BACK-END WITH YII
Author : Nguyen Van Cuong
INTRODUCTION
 Large applications are often divided into front-end
and back-end (or even more ends) depending on
the targe...
 Example :
Need build a custom school system,
This have a front end and back end.
front end:
teachers,
students
parents
-...
HOW ?
 Have many another way to organize directories .
 One way to organize directories using
WebApplicationEnd behavior.
 Front-end SiteController and all front-end views are
files generated by yiic tool. You have to create
back-end SiteContr...
STEP 1
 webroot/protected/config/admin.php :
return CMap::mergeArray(
require(dirname(__FILE__).'/main.php'),
array(
// P...
 By default, Yii will try to find controllers and views in
protected/controllers and protected/views
directories respecti...
class WebApplicationEndBehavior extends CBehavior {
private $_endName;
public function getEndName() {
return $this->_endNa...
public function onModuleCreate($event) {
$this->raiseEvent('onModuleCreate', $event);
}
protected function changeModulePat...
 Now add some lines to the main config file:
'behaviors'=>array(
'runEnd'=>array(
'class'=>'application.components.WebApp...
STEP 3
 Finally, let's protect back-end by creating a parent
controller for all back-end controllers
 webroot/protected/...
BENEFITS
 The advantages of this Organize directories is :
controller and the view of the two part - and page
admin serve...
THANK YOU !
Organize directories for applications with front-end and back-end with yii - CuongNV
Upcoming SlideShare
Loading in …5
×

Organize directories for applications with front-end and back-end with yii - CuongNV

2,752 views

Published on

Author : Nguyen Van Cuong

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,752
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
33
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Organize directories for applications with front-end and back-end with yii - CuongNV

  1. 1. ORGANIZE DIRECTORIES FOR APPLICATIONS WITH FRONT-END AND BACK-END WITH YII Author : Nguyen Van Cuong
  2. 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. 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. 4. HOW ?  Have many another way to organize directories .  One way to organize directories using WebApplicationEnd behavior.
  5. 5.  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:
  6. 6. 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…
  7. 7.  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
  8. 8. 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(); }
  9. 9. 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; }
  10. 10.  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".
  11. 11. 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:
  12. 12. 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 !
  13. 13. THANK YOU !

×