╃巡洋艦㊣ 2011-03-09 21:56:44 16749次浏览 46条回复 2 0 0

此方法参考官方网站的cookbook,详细请看Organize directories for applications with front-end and back-end, 我在此基础上做了一些改动,人个感觉非常棒了,大家可以试一下!

首先我把目录结构写下来:

www root/
    index.php
    admin.php
    assets/
    images/
    protected/
        config/
            main.php
        components/
        controllers/
        models/
        views/
        runtime/
        admin/
            config/
                main.php
            components/
            controllers/
            views/
            runtime/

前后台引导文件分别如下
// index.php:
require('path/to/yii.php');
Yii::app()->createWebApplication('protected/config/main.php')->run();
// admin.php:
require('path/to/yii.php');
Yii::app()->createWebApplication('protected/admin/config/main.php')->run();

cookbook上的方法是前后台各有一个配置文件,这样配置比较麻烦一些,我们可以采用覆盖的方法,这是参考一个回复者的方法,我又做了一些修改.

protected/admin/config/main.php的代码如下:

<?php
$backend=dirname(dirname(__FILE__));
$frontend=dirname($backend);
Yii::setPathOfAlias('backend', $backend);
$frontendArray=require($frontend.'/config/main.php');
$backendArray=array(
  'name'=>'网站后台管理系统',
  'basePath' => $frontend,
  'controllerPath' => $backend.'/controllers',
  'viewPath' => $backend.'/views',
  'runtimePath' => $backend.'/runtime',
  // autoloading model and component classes
  'import'=>array(
    'application.models.*',
    'application.components.*',
    'application.extensions.*',
    'application.extensions.nestedset.*',
    'backend.models.*',
    'backend.components.*', //这里的先后顺序一定要搞清
  ),
  'components'=>array(
    'user'=>array(
    // enable cookie-based authentication
    'allowAutoLogin'=>true,
    ),
  ),
  // main is the default layout
  //'layout'=>'main',
  // alternate layoutPath 'layoutPath'=>dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'views'.DIRECTORY_SEPARATOR.'layouts'.DIRECTORY_SEPARATOR,
);
if(!function_exists('w3_array_union_recursive'))
{
  /**
    * This function does similar work to $array1+$array2,
    * except that this union is applied recursively.
    * @param array $array1 - more important array
    * @param array $array2 - values of this array get overwritten
    * @return array
  */
  function w3_array_union_recursive($array1,$array2)
  {
    $retval=$array1+$array2;
    foreach($array1 as $key=>$value)
    {
      if(is_array($array1[$key]) && is_array($array2[$key]))
      $retval[$key]=w3_array_union_recursive($array1[$key],$array2[$key]);
    }
    return $retval;
  }
}

return w3_array_union_recursive($backendArray,$frontendArray);

这里我们的model是公用的,controller和view是分开的,我们还可以通过命令行对后台进行model和crud,方法如下:

>yiic shell path/to/site/admin.php
>model Article
>crud Article

这样后台对应的controller和view就生成了! 如果只对后台的变量进行配置的话,只需要修改protected/admin下的配置文件就可以了! 大家如果有不明白的地方,欢迎讨论!

您需要登录后才可以回复。登录 | 立即注册