Changes
Title
unchanged
Organize directories for applications with front-end and back-end using WebApplicationEnd behavior
Category
unchanged
Tutorials
Yii version
unchanged
Tags
unchanged
Content
changed
[...]
login.php
~~~
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 (or just copy and modify front-end ones).
> Note: Since a new full path to front-end layouts is "application.views.front.layouts", you have to edit "column1.php" and "column2.php" files to render "application.views.front.layouts.main" as a parent layout. Also set Controller's (protected/components/Controller.php) layout property to 'column1'.
Now let's create different config files for both ends. Since these files usually have much in common, we will "inherit" them from the main.php config:[...]
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.
Actually we can do it in the -end's config file by setting "viewPath" and "controllerPath" properties, but what if we are going to have some modules like News, Articles, etc.? We'll need to set these properties for them too!. We can also have some back-end modules which don't need such separation.
Here comes the Yii magic. In protected/components directory create a file "WebApplicationEndBehavior.php" with the following contents:[...]
// We can configure our module depending on the value
// of Yii::app()->endName.
$this->foo = (Yii::app()->endName == 'front') ? 'bar1' : 'bar2';
// Raise onModuleCreate event.[...]
Note that in this case the module's controllers and views paths must be organized as shown before.
If a module doesn't need a separation
to
f back-end and front-end controllers and views, then just omit
the onModuleCreate event's raising.
Finally, let's protect back-end by creating a parent controller for all back-end controllers:[...]
webroot/protected/controllers/back/SiteController.php must extend this controller to perform access checking.
Everything's done. New index.php and backend.php files look likare:
<strong>webroot/index.php:</strong>[...]
```
<strong>CRUD management as frontend and backend</strong>
For backend crud generation run url as,<br />
/backend.php?r=gii<br />
controller generate as : controllers\back\XyzController.php<br />
view files generate as : views\back\xyz\files.php<br />
<br />
For frontend crud generation run url as,<br />
/index.php?r=gii/crud<br />
controller generate as : controllers\front\XyzController.php<br />
view files generate as : views\front\xyz\files.php<br />
## Summary
The created behavior delivers us from specifying controllers and views paths for the application and all it's modules by using runEnd() method and invoking the onModuleCreate event in necessary places.[...]
```
But make sure that you insert these rules before news rules containing <_c> and <_a>. Otherwise the latter rules will be used instead.