用户
 找回密码
 入住 CI 中国社区
搜索
查看: 2886|回复: 3
收起左侧

第一次使用Codeigniter,大家看看我做的如何

[复制链接]
发表于 2012-5-5 15:35:14 | 显示全部楼层 |阅读模式
本帖最后由 faiinlove 于 2012-5-5 15:36 编辑

先来个控制器重写的简要代码
PHP复制代码
<?php
/**
 * 扩展Controller类,主要做了几个插入点,实际应用中还会加入登录信息,验证方法等等
 *  
 * @author gray.liu
 * @email gaoomei@gmail.com
 * @date 2012-4-27
 */

class MY_Controller extends CI_Controller{
       
        /**
         * 控制器名称
         * @var string
         */

        protected $_controller;
        /**
         * 执行的方法名称
         * @var string
         */

        protected $_action;
        /**
         * 渲染视图名称,如:index => index.php
         * @var string
         */

        protected $_render_action;
        /**
         * 是否取消视图渲染
         * @var bool
         */

        protected $_no_render = false;
       
        /**
         * 不允许子类重写构造方法,用_init方法代替
         */

        public final function __construct(){
                parent::__construct();
                $this->_controller = get_class($this);
                $this->_action = $this->router->fetch_method();
                $this->_init();
        }
       
        /**
         * 初始化,用于代替子类的构造方法
         */

        protected function _init(){}
       
        /**
         * 分发action前的操作,比如验证action执行权限
         */

        protected function _prePostDispatch(){
        }
       
        /**
         * 分发action后操作,如自动载入视图
         */

        protected function _postDispatch(){
                $this->_renderView();
        }
       
        /**
         * 重写
         * @param string $method
         * @param array $params
         */

        public final function _remap($method,$params = array()){
                $self = $this;
                if(!method_exists($self, $method)){
                        show_404();
                }
                $this->_action = $method;
                $this->_render_action = $method;
               
                $this->_prePostDispatch();
                call_user_func_array(array($self, $method), $params);
                $this->_postDispatch();
        }
       
        /**
         * 加载view视图,约定视图命名为(小写):controller/action.php
         * @param string $view_path
         */

        protected function _renderView($view_path = null){
                //不渲染视图
                if(empty($view_path) && $this->_no_render){
                        return;
                }
               
                //@TODO 加载公用头部
               
                //加载内容部分
                if(!empty($view_path)){
                        $this->_renderScript($view_path);
                }else{
                        $this->_renderScript(strtolower($this->_controller).'/'.$this->_render_action.'.php');
                }
               
                //@TODO 加载公用尾部
        }
       
        /**
         * 渲染渲染某个action对应的视图
         * @param string $action
         */

        protected function _renderAction($action){
                $this->_render_action = $action;
        }
       
        /**
         * 不渲染视图
         * @param bool $flag
         */

        protected function _setNoRender($flag = true){
                $this->_no_render = $flag;
        }
       
        /**
         * 加载视图脚本
         * @param unknown_type $script_path
         */

        protected function _renderScript($script_path){
                $this->load->view($script_path);
        }
       
       
}
 
复制代码


项目架构是这样的:
View《- Controller -》Service-》Model

发表于 2012-5-5 23:32:59 | 显示全部楼层
顶一下
 楼主| 发表于 2012-5-6 13:17:48 | 显示全部楼层
mackxu 发表于 2012-5-5 23:32
顶一下

谢谢!
发表于 2012-10-11 15:50:35 | 显示全部楼层
我直接改写ci的控制器/core/contrellor
PHP复制代码
 
class CI_Controller {
 
        private static $instance;
 
        /**
         * Constructor
         */

        public function __construct()
        {
                self::$instance =& $this;
               
                // Assign all the class objects that were instantiated by the
                // bootstrap file (CodeIgniter.php) to local class variables
                // so that CI can run as one big super object.
                foreach (is_loaded() as $var => $class)
                {
                        $this->$var =& load_class($class);
                }
 
                $this->load =& load_class('Loader', 'core');
 
                $this->load->initialize();
               
                log_message('debug', "Controller Class Initialized");
        }
 
        public static function &get_instance()
        {
                return self::$instance;
        }
       
        /**
         * 扩展ci处理视图功能,提供类似嵌入式布局,SEO优化
         * $data array要传递到视图的数据
         * $template string内容模板
         * $seokey string使用的seo数据在配置文件里的键名索引,默认不使用
         * $rtemplate array替换默认模板,可选索引参数,header,footer
         */

        public function render($template, $data = array(), $seokey = null, $rtemplate = array())
        {
                // layout
                $layout = array(
                        'header' => isset($rtemplate['header']) ? $rtemplate['header'] : 'common/header',
                        'content' => $template,
                        'footer' => isset($rtemplate['footer']) ? $rtemplate['footer'] : 'common/footer',
                );
               
                // seo
                // _title, _keywords, _description为保留关键字
                $seo_data = array(
                        '_title' => '',
                        '_keywords' => '',
                        '_description' => ''
                );
                // need seo
                if($seokey != null) {
                        $this->config->load('xconfig/seo', true, true);
                        $seo_data = $this->config->item($seokey, 'xconfig/seo');
                }
                // merge the data
                $data = array_merge($data, $seo_data);
                // load view
                foreach($layout as $key => $value) {
                        $this->load->view($value, $data);
                }
 
        }
}
 
复制代码

本版积分规则