zoukankan      html  css  js  c++  java
  • YII的Modules模块化

    转载来源:

    http://blog.csdn.net/mengxiangbaidu/article/details/7041296

    http://blog.csdn.net/colzer/article/details/8443042

    在YII中,程序入口是一个继承CApplication的CWebApplication的应用程序,在一个web请求的整个过程中,控制器,模型和视图都是由Application进行创建和控制。首先我们来看一下CWebApplication的类的继承结构:

    从上面我们可以看到CWebApplication本身也是一个CModue。在YII中,模块之间是一个树形结构。即每一个模块都可以包含多个子模块,每一个子模块可以继续包含子模块.其中APP为树的头节点,如图:

    对于一个具体请求,假设route=A/B/C/D,下面我们讲述一下,APP怎么选择相应的模块和模块中的控制器Controller和动作Action。具体的流程图如下:

     一个相对来说大的项目。如果按照yii生成的webapp进行开发。所有的controller放到controllers文件夹下,所有的model放到models文件夹下面,如果你有n多个controller和n多的model,是不是就显得这种组织结构过于繁琐,冗余了。还好YII支持Modules结构。你的项目可以分成n多的Module,然后每一个Module有自己的controllers和models。这样的组织结构,无论是开发,管理都方便简洁多了。看看YII的Modules的是组织方式和使用方法。

    │   ├── models
    │   │   ├── ContactForm.php
    │   │   ├── LoginForm.php
    │   │   └── User.php................................................................
    │   ├── modules模块的存放目录
    │   │   └── testmod一个模块,模块的名字对应是目录的名字,唯一。也是路由中的moduleid
    │   │       ├── components模块用到的组件
    │   │       ├── controllers包含控制器
    │   │       │   └── DefaultController.php默认控制器
    │   │       ├── messages国际化
    │   │       ├── models模型类文件
    │   │       ├── TestmodModule.php模块的类文件
    │   │       └── views试图文件
    │   │           ├── default默认视图
    │   │           │   ├── index.php视图文件
    │   │           └── layouts包含布局文件
    │   ├── runtime....................................................................
    │   │   └── application.log
    │   ├── tests
    │   │   ├── bootstrap.php
    │   │   ├── fixtures
    │   │   │   └── tbl_user.php

    模块的类文件:

    <?php
    
    class AdminModule extends CWebModule
    {
        public $defaultController = 'default';
        public $assetsUrl;  //样式目录
        public function init()
        {        
            // this method is called when the module is being created
            // you may place code here to customize the module or the application
    
            // import the module-level models and components
            $this->setImport(array(
                'application.extensions.*',
                'admin.models.*',
                'admin.components.*',
            ));
            
            //这里是用来区分前后台的session,为用户名session添加了一个admin
            Yii::app()->setComponents(array(
                'user' => array(
                    'stateKeyPrefix' => 'admin',
                    'loginUrl'=>Yii::app()->createUrl('/admin/default/login'),                                    
                ),
                'errorHandler'=>array(
                // use 'site/error' action to display errors
                    'errorAction'=>'/admin/default/error',
                ),
            )
            );
            //设定跳转url
            Yii::app()->user->setReturnUrl(Yii::app()->createUrl('admin'));
            
            //发布样式资源
            $this->assetsUrl = Yii::app()->getAssetManager()->publish(Yii::getPathOfAlias('application.modules.admin.assets'));
        }
    
        public function beforeControllerAction($controller, $action)
        {
            if(parent::beforeControllerAction($controller, $action))
            {
                // this method is called before any module controller action is performed
                // you may place customized code here            
                return true;
            }
            else
                return false;
        }
        
        
    }

    config配置:

    'modules'=>array(
            // uncomment the following to enable the Gii tool
            
            'gii'=>array(
                'class'=>'system.gii.GiiModule',
                'password'=>'Enter Your Password Here',
                // If removed, Gii defaults to localhost only. Edit carefully to taste.
                'ipFilters'=>array('127.0.0.1','::1'),
            ),
            
            'admin'=>array(
                'class' => 'application.modules.admin.AdminModule'        
            )
            
        ),

    通过gii配置:

    新加模块访问地址为:http://website/index.php?r=admin

  • 相关阅读:
    自定义View的ToolBar布局报错Error:(2) No resource identifier found for attribute 'context' in package 'c
    在学git之主分支 branch
    获取发布版SHA1
    关于开启线程与UI的操作
    播放音频和视频(VideoView控件)
    通知栏Notification的应用
    Android 真机调式 Installation failed with message 远程主机强迫关闭了一个现有的连接。. It is possible that this issue is resolved by uninstalling an existing version of the apk if it is present, and then re-installing. WA
    运行程序申请危险权限
    mysql乐观锁总结和实践
    Nginx配置文件nginx.conf中文详解
  • 原文地址:https://www.cnblogs.com/dcb3688/p/4607966.html
Copyright © 2011-2022 走看看