Codeigniter从控制器调用控制器

Codeigniter从控制器调用控制器,codeigniter,controller,Codeigniter,Controller,在最后两条评论之后,我将抛出我的真实代码,也许这会有所帮助: 这是着陆控制器: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Businessbuilder extends CI_Controller { function __construct() { parent::__construct(); } function

在最后两条评论之后,我将抛出我的真实代码,也许这会有所帮助:

这是着陆控制器:

 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Businessbuilder extends CI_Controller {

    function __construct()
    {
        parent::__construct();
    }
    function index()
    {
        $RTR = $GLOBALS["RTR"];


        // import the necessary libraries
        $this->load->model("site_pages");

        $RTR = $GLOBALS["RTR"];

        // get the current site
        $site = current_site();

        // get the requesting url
        $class = $RTR->uri->rsegments[1];
        $function = $RTR->uri->rsegments[2];

        // get the current function and class
        $current_method = explode("::", __METHOD__);

        // get the real class name that is going to be called
        $site_page = $this->site_pages->get(array("display_name"=>$class, "id"=>$site->id));
        $site_page = $site_page->result();
        if(count($site_page) == 1)
        {
            $site_page = $site_page[0];

            // set the class name to be called
            $class = $site_page->class_name;
        }

        // only execute if the requested url is not the current url
        if(!(strtolower($class) == strtolower($current_method[0]) && strtolower($function) == strtolower($current_method[1])))
        {
            if(!file_exists(APPPATH.'controllers/'.$RTR->fetch_directory().$class.EXT))
            {
                show_404($RTR->fetch_directory().$class);
                exit;
            }

            // include the required file. I use require once incase it is a file that I've already included
            require_once(APPPATH.'controllers/'.$RTR->fetch_directory().$class.EXT);

            // create an instance of the class
            $CI = new $class();

            if(method_exists($CI, $function))
                // call the method
                call_user_func_array(array(&$CI, $function), array_slice($RTR->uri->rsegments, 2));
            else
            {
                show_404($RTR->fetch_directory().$class);
                exit;
            }
        }
    }
}
或者这个(错误2):

我之所以会出现这些错误,是因为对象的实例与加载模型和库的实例不同。但奇怪的是,数组是带过去的,而不是对象。因此,在codeigniter数组的core Loader.php中,$\u ci\u模型填充了未加载到Public\u homepage类中的模型

另外,从第一次使用businessbuilder类开始,我就能够成功地加载和使用模块,但是当调用Public_homepage时,事情就开始失败了

让人困惑的是,我试图用一个问题找出两个错误,这可能是我的错误。以下是我获取错误的时间描述:

错误1:

当我按原样运行代码时,我无法调用sites属性

错误2:

当我换衣服的时候 调用用户函数数组(数组(&$CI,$function),数组切片($RTR->uri->rsegments,2)); 到 eval($class.->“$function)

我知道这确实令人困惑,特别是当我解释它时,但如果你需要更多信息,请让我知道。还要注意,公共_主页看起来是这样的,因为我正在测试。如果可以用最少的代码生成错误,则无需转储更多无用的行

更新 在阅读了一些答案后,我意识到我没有解释代码。这段代码的作用是允许我在数据库中存储不同的URL,但是存储在那里的所有URL都可以调用同一个页面,即使它们是不同的。我想一个确切的例子就是改变wordpress上的slug


发生的情况是,businessbuilder类被设置为接受对服务器的所有请求。当它点击businessbuilder类时,它将访问数据库,找出您正在使用的子url,找到用户正在寻找的真正控制器,并访问该控制器

经过大量的搜索,我想我找到了一个解决办法。问题是我对这个例子的想法。在深入研究该框架之后,我意识到它将实例存储为static var,private static$instance。我修改了构造函数,使其在该变量已填充时不会覆盖。除此之外,由于加载过程中仍然存在一些奇怪的情况,出于某种原因,对象会被标记为已加载,但实际上并非如此,因此我必须向控制器添加一个新的var,即受保护的$ci_实例。最后,我对CI_控制器进行了如下修改:

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * CodeIgniter
 *
 * An open source application development framework for PHP 5.1.6 or newer
 *
 * @package     CodeIgniter
 * @author      ExpressionEngine Dev Team
 * @copyright   Copyright (c) 2008 - 2011, EllisLab, Inc.
 * @license     http://codeigniter.com/user_guide/license.html
 * @link        http://codeigniter.com
 * @since       Version 1.0
 * @filesource
 */

// ------------------------------------------------------------------------

/**
 * CodeIgniter Application Controller Class
 *
 * This class object is the super class that every library in
 * CodeIgniter will be assigned to.
 *
 * @package     CodeIgniter
 * @subpackage  Libraries
 * @category    Libraries
 * @author      ExpressionEngine Dev Team
 * @link        http://codeigniter.com/user_guide/general/controllers.html
 */
class CI_Controller {

    private static $instance;
    protected $ci_instance; // line added

    /**
     * Constructor
     */
    public function __construct()
    {

        if(self::$instance == null) // line added
            self::$instance =& $this;

        $this->ci_instance =& get_instance(); // line added

        // 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->_base_classes =& is_loaded();

        $this->load->_ci_autoloader();

        log_message('debug', "Controller Class Initialized");
    }

    public static function &get_instance()
    {
        return self::$instance;
    }
}
// END Controller class

/* End of file Controller.php */
/* Location: ./system/core/Controller.php */

在应用程序/autoload.php中,将codeigniter设置为加载数据库类

$autoload['libraries'] = array('database', 'otherlibrary', 'otherlibrary2');

这一定是你解决问题所需要的一切。

如果你只使用HMVC

Class Models extends MX_Loader{

   function getUser($username){
       $sql="SELECT
                   *
              FROM
                   user
              WHERE username = ? "
       return $this->db->query($sql,array($username))->row();
   }
}

仅仅将$this->t()调用到索引方法中还不够吗?描述非常混乱,到处都是。请发布准确的错误消息,还有“最初我有点像”的代码是什么?你到底在那里干什么?当您将
$class
强制转换为对象时,如何将其与附加函数一起使用?这里真的不清楚……是的,但这只是一个例子。在真正的程序中,它将是一个完全不同的类。这类似于动态创建类实例的第二段代码。。。。什么是
$this->authenticate=false
$authenticate
变量在哪里?你是如何铸造一个“控制器”的?我想你对你正在做的事感到困惑。。。请用这个代码解释你的意图,因为它是非常不正确的。那是旧代码$此->身份验证已添加到基本控制器文件中,因为它正在构造函数中执行某些身份验证。如果您同意该解决方案,则始终可以使用自己的控制器覆盖CI_控制器,这样您就不会修改CI核心类,也不会出现升级问题。你在这里有这些信息:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: Businessbuilder::$db

Filename: core/Model.php

Line Number: 50
Fatal error: Call to a member function query() on a non-object in /var/www/businessbuilderapp.com/public_html/application/models/bba_model.php on line 25 
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * CodeIgniter
 *
 * An open source application development framework for PHP 5.1.6 or newer
 *
 * @package     CodeIgniter
 * @author      ExpressionEngine Dev Team
 * @copyright   Copyright (c) 2008 - 2011, EllisLab, Inc.
 * @license     http://codeigniter.com/user_guide/license.html
 * @link        http://codeigniter.com
 * @since       Version 1.0
 * @filesource
 */

// ------------------------------------------------------------------------

/**
 * CodeIgniter Application Controller Class
 *
 * This class object is the super class that every library in
 * CodeIgniter will be assigned to.
 *
 * @package     CodeIgniter
 * @subpackage  Libraries
 * @category    Libraries
 * @author      ExpressionEngine Dev Team
 * @link        http://codeigniter.com/user_guide/general/controllers.html
 */
class CI_Controller {

    private static $instance;
    protected $ci_instance; // line added

    /**
     * Constructor
     */
    public function __construct()
    {

        if(self::$instance == null) // line added
            self::$instance =& $this;

        $this->ci_instance =& get_instance(); // line added

        // 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->_base_classes =& is_loaded();

        $this->load->_ci_autoloader();

        log_message('debug', "Controller Class Initialized");
    }

    public static function &get_instance()
    {
        return self::$instance;
    }
}
// END Controller class

/* End of file Controller.php */
/* Location: ./system/core/Controller.php */
$autoload['libraries'] = array('database', 'otherlibrary', 'otherlibrary2');
Class Models extends MX_Loader{

   function getUser($username){
       $sql="SELECT
                   *
              FROM
                   user
              WHERE username = ? "
       return $this->db->query($sql,array($username))->row();
   }
}