[Yii Framework] Yii如何实现前后台的session分离

Yii Framework实现前后台frontend,backend分离的方法有几种,总结如下:

1. 分开入口文件

目录结构

index.php
admin.php
assets/
...其它目录
protected/
    config/
        main.php
    components/
    controllers/
    ...其它目录    
    admin/
        config/
            main.php
        components/
        controllers/
        ...其它目录

网站正常的入口文件是index.php,我们加多一个admin.php用来处理后台事务

// 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();

protected/admin/config/main.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,
);

return CMap::mergeArray($frontendArray, $backendArray);

2. 后台admin作为一个module存在

目录结构如下

protected/
    commands/
    components/
    ...其它目录
    modules/
      admin/
        assets/
        components/
        controllers/
        ...其它目录

可以通过设置user这个组件来实现前后台的session id分开

protected/modules/admin/AdminModule.php

public function init()
{
    //...其它代码
    Yii::app()->setComponents(
        array(
            //...其它组件设置代码
            'user' => array(
                'stateKeyPrefix' => '__admin__',//设置session前缀,以分开不同module
            ),
    ));
}

3. 使用架构分离

其实这个跟第一种方法类似,目录结构如下

backend/
    components/
config/
        environments/
            main-private.php *
            main-prod.php
            params-private.php *
            params-prod.php
    main-env.php *
    main-local.php *
    main.php
    params-env.php *
    params-local.php *
    params.php
    test.php
controllers/
    SiteController.php
    ...
extensions/
        behaviors/
        validators/
    lib/
    models/
        FormModel.php
        ...
    modules/
    runtime/ *
    views/
        layouts/
        site/
    widgets/
    www/
        assets/ *
        css/
        images/
        js/
        themes/
        index.php
        .htaccess
common/
    components/
    config/
        environments/
            params-private.php *
            params-prod.php
        params-env.php *
        params-local.php *
        params.php
    data/
    extensions/
        behaviors/
        validators/
    lib/
        Behat/
        Pear/
        Yii/
        Zend/
    messages/
    models/
    widgets/
console/
    commands/
    components/
    config/
        environments/
    lib/
    migrations/
    models/
    runtime/ *
    yiic.php
frontend/
    components/
    config/
        environments/
            main-private.php *
            main-prod.php
            params-private.php *
            params-prod.php
        main-env.php *
        main-local.php
        main.php
        params-env.php *
        params-local.php *
        params.php
        test.php
    controllers/
    extensions/
        behaviors/
        validators/
    lib/
    models/ 
    modules/    
    runtime/ *
    views/
        layouts/
        site/
    www/
        assets/ *
        css/
        files/
        images/
            js/
            less/
        index.php
        robots.txt
        .htaccess
tests/
    bootstrap/
        FeatureContext.php
        YiiContext.php
    features/
        Startup.feature
    behat.yml

INSTALL.md
README.md
runbehat
runpostdeploy
yiic
yiic.bat

github上面有这样一个demo架构,推荐那些有自己服务器的人采用这种架构,具体地址:https://github.com/clevertech/YiiBoilerplate

 

Have fun with Yii!

posted @ 2013-05-04 23:51  DavidHHuan  阅读(1603)  评论(4编辑  收藏  举报