Yii 1.1: Organize directories for applications with front-end and back-end using WebApplicationEnd behavior

40 followers

Previously there was described a way to build an application with front-end and back-end. I would like to continue this theme and suggest another way to organize directories using WebApplicationEnd behavior.

If you are not familiar with events & behaviors yet, then please, read this tutorial to get basic knowledge about using them.

First, create a new web application with yiic tool. I used an application generated by Yii 1.1.1, but everything described below should work with other versions of Yii Framework (probably with minor changes).

Now let's modify some directories' structure to separate front-end and back-end related files.

Since application's ends usually use the same models, but different controllers and views, we will separate them by creating two subdirectories under protected/controllers and protected/views directories:

webroot/
    protected/
        components/
            Controller.php
            UserIdentity.php
        controllers/
            /front
                SiteController.php
            /back
                SiteController.php
        views/
            /front
                /layouts
                    column1.php
                    column2.php
                    main.php
                /site
                   /pages
                       about.php
                   contact.php
                   error.php
                   index.php
                   login.php
            /back
                /layouts
                    main.php
                /site
                    error.php
                    index.php
                    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:

webroot/protected/config/front.php:

return CMap::mergeArray(
    require(dirname(__FILE__).'/main.php'),
    array(
        // Put front-end settings there
        // (for example, url rules).
    )
);

webroot/protected/config/back.php:

return CMap::mergeArray(
    require(dirname(__FILE__).'/main.php'),
    array(
        // Put back-end settings there.
    )
);

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:

class WebApplicationEndBehavior extends CBehavior
{
    // Web application end's name.
    private $_endName;
 
    // Getter.
    // Allows to get the current -end's name
    // this way: Yii::app()->endName;
    public function getEndName()
    {
        return $this->_endName;
    }
 
    // Run application's end.
    public function runEnd($name)
    {
        $this->_endName = $name;
 
        // Attach the changeModulePaths event handler
        // and raise it.
        $this->onModuleCreate = array($this, 'changeModulePaths');
        $this->onModuleCreate(new CEvent($this->owner));
 
        $this->owner->run(); // Run application.
    }
 
    // This event should be raised when CWebApplication
    // or CWebModule instances are being initialized.
    public function onModuleCreate($event)
    {
        $this->raiseEvent('onModuleCreate', $event);
    }
 
    // onModuleCreate event handler.
    // A sender must have controllerPath and viewPath properties.
    protected function changeModulePaths($event)
    {
        $event->sender->controllerPath .= DIRECTORY_SEPARATOR.$this->_endName;
        $event->sender->viewPath .= DIRECTORY_SEPARATOR.$this->_endName;
    }
}

Now add some lines to the main config file:

'behaviors'=>array(
    'runEnd'=>array(
        'class'=>'application.components.WebApplicationEndBehavior',
    ),
),

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

If you have a module, which should use different controllers and views for different ends, then just modify it's init() method:

protected function init()
{
    // ...    
 
    // We can configure our module depending on the value
    // of Yii::app()->endName.
    $this->foo = (Yii::app()->endName == 'front') ? 'bar1' : 'bar2';
 
    // Raise onModuleCreate event.
    Yii::app()->onModuleCreate(new CEvent($this));
}

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 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/components/BackEndController.php:

class BackEndController extends CController
{
    public $layout='layout_name';
    public $menu=array();
    public $breadcrumbs=array();
 
    public function filters()
    {
        return array(
            'accessControl',
        );
    }
 
    public function accessRules()
    {
        return array(
            array('allow',
                'users'=>array('*'),
                'actions'=>array('login'),
            ),
            array('allow',
                'users'=>array('@'),
            ),
            array('deny',
                'users'=>array('*'),
            ),
        );
    }
}

webroot/protected/controllers/back/SiteController.php must extend this controller to perform access checking.

Everything's done. New index.php and backend.php files are:

webroot/index.php:

$yii = dirname(__FILE__).'/../yii/framework/yii.php';
$config = dirname(__FILE__).'/protected/config/front.php';
 
// Remove the following lines when in production mode
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL', 3);
 
require_once($yii);
Yii::createWebApplication($config)->runEnd('front');

webroot/backend.php:

$yii = dirname(__FILE__).'/../yii/framework/yii.php';
$config = dirname(__FILE__).'/protected/config/back.php';
 
// Remove the following lines when in production mode
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL', 3);
 
require_once($yii);
Yii::createWebApplication($config)->runEnd('back');

CRUD management as frontend and backend

For backend crud generation run url as,
/backend.php?r=gii
controller generate as : controllers\back\XyzController.php
view files generate as : views\back\xyz\files.php

For frontend crud generation run url as,
/index.php?r=gii/crud
controller generate as : controllers\front\XyzController.php
view files generate as : views\front\xyz\files.php

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.

Also modules became more self-sufficient and can be easily integrated with the existing front-end and back-end layouts.

Appendix A. How to use yiic tool.

You can easily generate models and views for the front-end the old way without any changes. Just go to the application's webroot directory and execute the next command:

protected/yiic shell
or
path/to/php.exe protected/yiic shell

All generated models will be saved under protected/models directory, and controllers & views will be saved under protected/(controllers|views)/front directory.

Generating controllers and views for the back-end is a little different. You have to run yiic the next way:

protected/yiic shell backend.php
or
path/to/php.exe protected/yiic shell backend.php

Now all generated controllers and views will go to protected/(controllers|views)/back directory.

Appendix B. Back-end url rules.

Usually you don't need to setup url rules for the back-end, but if you want to do this, then you'll have to modify .htaccess as follows:

AddDefaultCharset utf-8

Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on

# Make the backend accessible via url: http://site/backend.
RewriteRule ^backend backend.php

# If a directory or a file exists, use it directly.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Otherwise forward it to index.php.
RewriteRule . index.php

Then, in the back.php config file setup url manager component:

'urlManager'=>array(
    'urlFormat'=>'path',
    'showScriptName'=>false,
    'rules'=>array(
        'backend'=>'site/index',
        'backend/<_c>'=>'<_c>',
        'backend/<_c>/<_a>'=>'<_c>/<_a>',
    ),
),

Now you can generate beautiful urls using CHtml::link() method.

If you have a module (e.g. news), then you'll need to add 3 more rules before existing ones:

'backend/news'=>'news',
'backend/news/<_c>'=>'news/<_c>',
'backend/news/<_c>/<_a>'=>'news/<_c>/<_a>',

Also, you still can add own rules:

'backend/news/<id:\d+>'=>'news/newsReport/update',

But make sure that you insert these rules before news rules containing <_c> and <_a>. Otherwise the latter rules will be used instead.

Total 20 comments

#16342 report it
banago at 2014/02/12 12:18pm
Needs some clarification on using modules

I don't understand exactly what the value of foo should be in the explained example.

I guess the bar1 is for frontend and bar2 for backend.

protected function init()
{
    // ...    
 
    // We can configure our module depending on the value
    // of Yii::app()->endName.
    $this->foo = (Yii::app()->endName == 'front') ? 'bar1' : 'bar2';
 
    // Raise onModuleCreate event.
    Yii::app()->onModuleCreate(new CEvent($this));
}
#15914 report it
mahmoud khafagy at 2013/12/29 05:16am
make this structure in module

hi ,everybody i need to make this structure in module like this

module

test

 controllers

     back
     front
#13653 report it
Yang He at 2013/06/14 05:51am
how to create URLs between ends?

Thanks for the great article.

Just wondering if there is a way to create URLs between two ends? For example, creating a URL pointing to a page in the frontend from backend?

I noticed some others raised the same question in the forum post but no answer. Many thanks!

#13459 report it
primico at 2013/05/29 04:31pm
SiteController cannot find the requested view "index".

Hi, great tutorial and I think I am really close to getting it working, except I get this one error on the frontend. It is probably something really simple I am missing but I am still trying to learn the yii framework so if anyone can point me in the right direction, I would be extremely grateful!!!

CException SiteController cannot find the requested view "index".

/var/www/[mysite]/protected/controllers/front/SiteController.php(33): CController->render("index") 28 public function actionIndex() 29 { 30 // renders the view file 'protected/views/site/index.php' 31 // using the default layout 'protected/views/layouts/main.php' 32
33 $this->render('index'); 34 } 35 36 /** 37 * This is the action to handle external exceptions. 38 */

#11088 report it
mahmoud khafagy at 2012/12/15 05:10pm
My url in backend is not correct

url in backend is not display url correct my config/main.php

'urlManager'=>array(
            'urlFormat'=>'path',
            'showScriptName'=>false,
            'rules'=>array(
                    '<controller:\w+>/<id:\d+>'=>'<controller>/view',
                    '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
                    '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
              ),
            ),

and my config/back.php

'urlManager'=>array(
            'urlFormat'=>'path',
            'showScriptName'=>false,
            'rules'=>array(
                'backend'=>'admin/login',
                'backend/<_c>'=>'<_c>',
                'backend/<_c>/<_a>'=>'<_c>/<_a>',
            ),
        ),

url in backend not display before url "backend"

#10324 report it
kiran sharma at 2012/10/19 09:42am
@fouss

any Yii user had questions/doubts about this wiki article, can move further with This Forum Post..

#10323 report it
fouss at 2012/10/19 09:06am
theming... @kiran sharma

I undersatand that and I tested it (working) but theming is causing problem to me... there should be a forum post where we could discuss deeply about all the aspect of this way of Organizing directories. I'll be happy if the author (andy_s) could update his wiki including theming (for admin and front users)

Thx!

#10319 report it
kiran sharma at 2012/10/19 08:11am
@fouss

this will use to seperate system in two part as admin side as "back" and user side as "front",

For more understanding you can refer This wiki article written by yii user vibhaJadwani

#10318 report it
fouss at 2012/10/19 07:36am
theming...

I could'nt understand theming part...

#9876 report it
giuseppelng at 2012/09/18 04:08pm
webapp from scratch

Hi, thanks for the tutorial. I would like to ask a question: what are the steps to create a new webapp from scratch, separating backend and frontend?

Thanks, Regards.

#9101 report it
vibhaJadwani at 2012/07/20 02:33am
Front and admin login issue

I was working with http://www.yiiframework.com/wiki/356/how-to-create-front-and-admin-side-login-form and i found that is we don't add different name in config file font.php & back.php i.e.

'name'=>'Website Admin',

it overrides front's login with admin vice-versa. If anybody is having session issue try to add 'name' in both config.

#8912 report it
Amjad Khan at 2012/07/07 12:18am
Thanks for the help

Thanks sir my problem has been resolved, thanks very much.

#8908 report it
hnam at 2012/07/06 03:29pm
Two separate themes for front and back

If you are using two separate themes for front and back in your 'Themes' folder.

now add following into protected/config/back.php otherwise it will

array(
  'theme'=>'back',
  #...
  'components'=>array(
     'urlManager'=>array(
     #...
     .............
         'rules'=>array(
           'backend'=>'site/index',     
           'backend/<controller:\w+>/<id:\d+>'=>'<controller>/view',
           'backend/<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
           'backend/<controller:\w+>/<action:\w+>'=>'<controller>/<action>',        
            ),
 
        ),
 
   ),
),

you can also add a theme name in front.php in same directory for front section.

#8900 report it
Amjad Khan at 2012/07/06 09:04am
Got Exception after setting up url manager component

if i paste ulrManager rules in the back file i got the following exception error "Property "CWebApplication.urlManager" is read only. "

#8438 report it
sirin k at 2012/06/03 11:22am
Very good article

Very good article.going to try this now.

#6898 report it
Sebastian K. at 2012/02/11 08:17am
Links from backend to frontend

What is the best approach to create Urls inside the backend web application that should link to frontend?

One abvious way is to hard code the frontend base url and building absolute urls. But this is not very elegant and ignores all the url rules defined in the frontend.

Any better ideas?

#5189 report it
kiskp at 2011/09/21 04:28pm
Theming

Be sure to set up a theme name in your config file

theme => 'your-theme-name';

Make the directory structure like in the protected's view folder

themes/
    your-theme-name/        
        views/
            /front
                /layouts
                    column1.php
                    column2.php
                    main.php
                /site
                   /pages
                       about.php
                   contact.php
                   error.php
                   index.php
                   login.php
            /back
                /layouts
                    main.php
                /site
                    error.php
                    index.php
                    login.php

Modify WebApplicationEndBehavior.php onModuleChange function

protected function changeModulePaths($event)
    {
        $event->sender->controllerPath .= DIRECTORY_SEPARATOR.$this->_endName;
        $event->sender->viewPath .= DIRECTORY_SEPARATOR.$this->_endName;
 
        if ($event->sender->theme !== null)
            $event->sender->viewPath = $event->sender->theme->basePath.DIRECTORY_SEPARATOR.'views'.DIRECTORY_SEPARATOR.$this->_endName;
    }
#3603 report it
mc.aser at 2011/04/21 08:44am
great job

nice tutorial, i've used it in more then 10 projects. but now i'm trying make some themes for one of this project and meet problem with setting up right config for theme. Could you give any advice how to handle themes in this architecture ?

If you want to keep everything in one theme

Create directories:

/
  themes/
    classic/
      css/
        main.css
      image/
        logo.jpg
      view/
        page/
          index.php
      backend/
        css/
          back.css
        image/
           back-logo.jpg
        viev/
          page/
             admin.php

And edit webroot/protected/config/back.php

'theme'=>'classic/backend'

Or create two different themes and indicate webroot/protected/config/back.php:

'theme'=>'backend'
#3317 report it
Sasha at 2011/04/02 07:58am
great job

nice tutorial, i've used it in more then 10 projects. but now i'm trying make some themes for one of this project and meet problem with setting up right config for theme. Could you give any advice how to handle themes in this architecture ?

#706 report it
Nique at 2010/03/15 06:54am
No need for another script in your root!

Hi there,

There is no need for another script in your root. If you have privileges to setup subdomains try this!

  • Setup a sub-domain (like http://backend.domain.com) and let it point to the same root.
  • Put the following code in the index.php bootstrap

    define('YII_DEBUG', true);
    require_once '../../lib/yii/yii.php';
    
    if(($sd = explode('.',$_SERVER['HTTP_HOST'])) && $sd[0] == 'backend')
        Yii::createWebApplication('./path/to/config/backend.php')->runEnd('back');
    else 
        Yii::createWebApplication('./path/to/config/frontend.php')->runEnd('front');

Leave a comment

Please to leave your comment.

Write new article
  • Written by: andy_s
  • Updated by: kiran sharma
  • Category: Tutorials
  • Yii Version: 1.1
  • Votes: +33
  • Viewed: 78,739 times
  • Created on: Feb 2, 2010
  • Last updated: Jun 27, 2012