• Yii Framework处理网站前后台文件的方法


    此方法参考官方网站的cookbook,详细请看http://www.yiiframework.com/doc/cookbook/33/,
    我在此基础上做了一些改动,人个感觉非常棒了,大家可以试一下!

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

    wwwroot/

        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的代码如下:

    $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下的配置文件就可以了!

  • 相关阅读:
    物联网浪潮之下如何从 0 到 1开启智能化硬件开发?
    安卓开发工程师应该这样快速使用机智云APP开源框架
    hdu 1246
    UVa202
    CodeForces
    热身经验被打脸总结大会感想
    多重背包的二进制优化
    背包问题+欧拉筛法
    HDU 1106
    HDU 1215
  • 原文地址:https://www.cnblogs.com/angela-dream/p/3534124.html
Copyright © 2020-2023  润新知