昨天提了个问题 说: ci怎么支持admin,member,home等多目录功能,今天本身折腾了下

1.配置application\config的config文件php

$config['enable_query_strings'] = true;
$config['controller_trigger'] = 'c';
$config['function_trigger'] = 'a';
$config['directory_trigger'] = 'm'; // 网站的模块,前台,后台,会员中心deng

2.配置configs下面的router.phphtml

$route['home']['default_controller'] = "home/welcome";
$route['home']['404_override'] = '';
$route['admin']['default_controller'] = "admin/index";
$route['admin']['404_override'] = '';
$route['member']['default_controller'] = "member/index";
$route['member']['404_override'] = '';

而后就开始修改core下面的router啦,在140行左右作修改app

// Load the routes.php file.
        if (defined('ENVIRONMENT') AND is_file(APPPATH . 'config/' . ENVIRONMENT . '/routes.php')) {
            include(APPPATH . 'config/' . ENVIRONMENT . '/routes.php');
           
        } elseif (is_file(APPPATH . 'config/routes.php')) {
            include(APPPATH . 'config/routes.php');
             //处理各个模块的route分发
            $m  = $_GET[$this->config->item('directory_trigger')]; //这里就取出了不一样模块的route配置啦
 
            $this->routes = (!isset($route[$m]) OR !is_array($route[$m])) ? array() : $route[$m];
        }

这样一配置,就能够进行控制器的分发了less

视图呢,同理可得啊,这是我修改过的加载视图的functionide

protected function _ci_load($_ci_data) {
        // Set the default data variables
        foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $_ci_val) {
            $$_ci_val = (!isset($_ci_data[$_ci_val])) ? FALSE : $_ci_data[$_ci_val];
        }


        $file_exists = FALSE;
        $_ci_ext = '.html';
        // Set the path to the requested file
        if ($_ci_path != '') {
            $_ci_x = explode('/', $_ci_path);
            $_ci_file = end($_ci_x) . $_ci_ext;
        } else {

            //能够注释掉下面这一行,由于咱们已经能够经过路由来自动加载视图
            //$_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION);//获取视图文件的后缀名,默认是不传递后缀名的
            //拼接视图路径
            //加载配置,以方便获取mca(m:模块,c:控制器,a:action)的值
            $this->config = &load_class('Config', 'core');
            $directory_trigger = $this->config->item('directory_trigger');
            $controller_trigger = $this->config->item('controller_trigger');
            $function_trigger = $this->config->item('function_trigger');

            $m = $_GET[$directory_trigger] ? $_GET[$directory_trigger] : 'home';
            $c = $_GET[$controller_trigger] ? $_GET[$controller_trigger] : 'index';
            $a = $_GET[$function_trigger] ? $_GET[$function_trigger] : 'index';
            //特殊处理common/header的状况
            $slasharr = explode('/', $_ci_view);
            if (count($slasharr) > 1) {
                $_ci_file = $m . '/' . $slasharr['0'].'/' .$slasharr['1'] . $_ci_ext;
            } else {
                //默认的视图名称(不包含后缀名)
                $view_name = $_ci_view == '' ? $a : $_ci_view; //若是没有传视图的名称,默认使用action的名称
                $_ci_file = $m . '/' . $c . '/' . $view_name . $_ci_ext;
            }
            foreach ($this->_ci_view_paths as $view_file => $cascade) {
                    if (file_exists($view_file . $_ci_file)) {
                        $_ci_path = $view_file . $_ci_file;
                        $file_exists = TRUE;
                        break;
                    }

                    if (!$cascade) {
                        break;
                    }
                }
        }

        if (!$file_exists && !file_exists($_ci_path)) {
            show_error('Unable to load the requested file: ' . $_ci_file);
        }

        // This allows anything loaded using $this->load (views, files, etc.)
        // to become accessible from within the Controller and Model functions.

        $_ci_CI = & get_instance();
        foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var) {
            if (!isset($this->$_ci_key)) {
                $this->$_ci_key = & $_ci_CI->$_ci_key;
            }
        }

        /*
         * Extract and cache variables
         *
         * You can either set variables using the dedicated $this->load_vars()
         * function or via the second parameter of this function. We'll merge
         * the two types and cache them so that views that are embedded within
         * other views can have access to these variables.
         */
        if (is_array($_ci_vars)) {
            $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
        }
        extract($this->_ci_cached_vars);

        /*
         * Buffer the output
         *
         * We buffer the output for two reasons:
         * 1. Speed. You get a significant speed boost.
         * 2. So that the final rendered template can be
         * post-processed by the output class.  Why do we
         * need post processing?  For one thing, in order to
         * show the elapsed page load time.  Unless we
         * can intercept the content right before it's sent to
         * the browser and then stop the timer it won't be accurate.
         */
        ob_start();

        // If the PHP installation does not support short tags we'll
        // do a little string replacement, changing the short tags
        // to standard PHP echo statements.

        if ((bool) @ini_get('short_open_tag') === FALSE AND config_item('rewrite_short_tags') == TRUE) {
            echo eval('?>' . preg_replace("/;*\s*\?>/", "; ?>", str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));
        } else {
            include($_ci_path); // include() vs include_once() allows for multiple views with the same name
        }

        log_message('debug', 'File loaded: ' . $_ci_path);

        // Return the file data if requested
        if ($_ci_return === TRUE) {
            $buffer = ob_get_contents();
            @ob_end_clean();
            return $buffer;
        }

        /*
         * Flush the buffer... or buff the flusher?
         *
         * In order to permit views to be nested within
         * other views, we need to flush the content back out whenever
         * we are beyond the first level of output buffering so that
         * it can be seen and included properly by the first included
         * template and any subsequent ones. Oy!
         *
         */
        if (ob_get_level() > $this->_ci_ob_level + 1) {
            ob_end_flush();
        } else {
            $_ci_CI->output->append_output(ob_get_contents());
            @ob_end_clean();
        }
    }

最后的文件目录,主要是application稍做修改了,其余不变post

--application网站

    ---controllerthis

        -----admindebug

        -----membercode

        -----home

    ---model

    --view

         -----admin

        -----member

        -----home

基本上是这样了,有什么很差的地方还请大神给点建议

下一步我想实现的功能是

http://www.test.com(主站)

http://member.test.com

http://admin.test.com

这种方式的二级域名也都走同一个入口文件,而不须要繁琐的配置,但愿大神给点建议

相关文章
相关标签/搜索