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

[核心代码 Core] Controller 使用另一个 Controller

[复制链接]
发表于 2013-4-13 22:38:35 | 显示全部楼层 |阅读模式
本帖最后由 smartweb 于 2015-11-23 12:31 编辑

谁说controllers不能使用另一个controllers呢?
新建一个MY_Loader.php在\application\core\MY_Loader.php
用法:(引用controllers下的homepage/articles2)
PHP复制代码
$this->load->controller('homepage/articles2','homepage');
$this->homepage->index($lang,$mtypeid);
复制代码


MY_Loader.php源文件:

PHP复制代码
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class MY_Loader extends CI_Loader
{
    protected $_my_controller_paths     = array();  
 
    protected $_my_controllers          = array();
 
 
    public function __construct()
    {
        parent::__construct();
 
        $this->_my_controller_paths = array(APPPATH);
    }
 
    public function controller($controller, $name = '', $db_conn = FALSE)
    {
        if (is_array($controller))
        {
            foreach ($controller as $babe)
            {
                $this->controller($babe);
            }
            return;
        }
 
        if ($controller == '')
        {
            return;
        }
 
        $path = '';
 
        // Is the controller in a sub-folder? If so, parse out the filename and path.
        if (($last_slash = strrpos($controller, '/')) !== FALSE)
        {
            // The path is in front of the last slash
            $path = substr($controller, 0, $last_slash + 1);
 
            // And the controller name behind it
            $controller = substr($controller, $last_slash + 1);
        }
 
        if ($name == '')
        {
            $name = $controller;
        }
 
        if (in_array($name, $this->_my_controllers, TRUE))
        {
            return;
        }
 
        $CI =& get_instance();
        if (isset($CI->$name))
        {
            show_error('The controller name you are loading is the name of a resource that is already being used: '.$name);
        }
 
        $controller = strtolower($controller);
$controller=ucfirst($controller);//ci3之后强制要求首字母大写了
        foreach ($this->_my_controller_paths as $mod_path)
        {
            if ( ! file_exists($mod_path.'controllers/'.$path.$controller.'.php'))
            {
                continue;
            }
 
            if ($db_conn !== FALSE AND ! class_exists('CI_DB'))
            {
                if ($db_conn === TRUE)
                {
                    $db_conn = '';
                }
 
                $CI->load->database($db_conn, FALSE, TRUE);
            }
 
            if ( ! class_exists('CI_Controller'))
            {
                load_class('Controller', 'core');
            }
 
            require_once($mod_path.'controllers/'.$path.$controller.'.php');
 
            $controller = ucfirst($controller);
 
            $CI->$name = new $controller();
 
            $this->_my_controllers[] = $name;
            return;
        }
 
        // couldn't find the controller
        show_error('Unable to locate the controller you have specified: '.$controller);
    }
 
}
 
复制代码



评分

参与人数 2威望 +6 收起 理由
大道达人 + 1 很给力!
Hex + 5 很给力!

查看全部评分

发表于 2015-3-31 15:11:29 | 显示全部楼层
fizz 发表于 2015-3-31 10:40
有没有model方法调用model方法的, 有时候一个model需要调用另一个model的结果

會出現這種情形
是你個人對 Model 的公有與私有配置不夠準確
发表于 2015-3-31 14:58:51 | 显示全部楼层
fizz 发表于 2015-3-31 10:40
有没有model方法调用model方法的, 有时候一个model需要调用另一个model的结果

通过控制器就可以了呀
发表于 2015-3-31 10:40:50 | 显示全部楼层
有没有model方法调用model方法的, 有时候一个model需要调用另一个model的结果
发表于 2013-4-14 09:26:19 | 显示全部楼层
精神可嘉  不过需要被其他controller引用的controller通常制作成 库 更合适

点评

正解  发表于 2013-10-29 08:57
 楼主| 发表于 2013-4-14 22:08:42 | 显示全部楼层
Altair 发表于 2013-4-14 09:26
精神可嘉  不过需要被其他controller引用的controller通常制作成 库 更合适

我程序已经开发好了,突然发现URL对SEO友好还有待提高,没时间重写了,只有加一个rout.php来调其它controller了。
发表于 2013-4-15 15:15:32 | 显示全部楼层
哈哈,同赞精神可嘉!帮楼主修改了高亮。
加分~
 楼主| 发表于 2013-4-15 23:22:35 | 显示全部楼层
Hex 发表于 2013-4-15 15:15
哈哈,同赞精神可嘉!帮楼主修改了高亮。
加分~

有点受宠了,俺还是初哥
发表于 2013-12-13 10:45:06 | 显示全部楼层
确实得高亮
发表于 2014-5-9 11:59:33 | 显示全部楼层
很好用。。。。谢谢
发表于 2015-3-10 16:11:10 | 显示全部楼层
搞下来 试试 谢谢分享

本版积分规则