Joh*_*ong 8 controller codeigniter
在最后两条评论之后,我将丢弃我的真实代码,也许它会有所帮助:
这是登陆控制器:
<?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;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是一个将被调用的动态控制器的示例:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Public_homepage extends CI_Controller {
function __construct()
{
parent::__construct();
}
function index()
{
echo "<br /><br /><br />";
$this->load->model("sites");
$style = $this->sites->get(array("id"=>1)); // fail here, sites not defined
//print_r($style);
exit;
$view_params = array();
$view_params["site_id"] = $this->site_id;
$this->load->view('public_homepage', $view_params);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我使用的模型:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Sites extends CI_Model
{
function __construct()
{
parent::__construct();
}
function get($search = array())
{
return $this->db->query("SELECT * FROM sites"); // failure on this line, db undefined
}
}
Run Code Online (Sandbox Code Playgroud)
我得到的错误是这个(error1):
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Public_homepage::$sites
Filename: controllers/public_homepage.php
Line Number: 15
Fatal error: Call to a member function get() on a non-object in /var/www/businessbuilderapp.com/public_html/application/controllers/public_homepage.php on line 15
Run Code Online (Sandbox Code Playgroud)
或者这个(error2):
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
Run Code Online (Sandbox Code Playgroud)
我的理由是为什么我得到这些错误是因为对象的实例不同于加载模型和库的实例.然而,有些奇怪的是,数组是被携带的,而不是对象.所以在codeigniter数组的核心Loader.php中,$ _ci_models填充了未在Public_homepage类中加载的模型
另外可能对你有帮助的是,从第一次通过businessbuilder类开始,我能够成功加载和使用模块,但是当调用Public_homepage时,那就是事情开始失败的时候.
令人困惑的是,我试图用一个问题找出2个错误,这可能是我的错误.这是我何时收到错误的说明:
ERROR1:
当我按原样运行代码时,我无法调用sites属性.
误差2:
当我更改call_user_func_array(array(&$ CI,$ function),array_slice($ RTR-> uri-> rsegments,2)); 到eval($ class." - >".$ function);
我知道这真的很混乱,特别是当我解释它时,但如果您需要更多信息,请告诉我.另请注意,Public_homepage看起来像是因为我正在测试.如果可以使用最少的代码生成错误,则无需转储更多无用的行.
在阅读了一些答案之后,我意识到我没有解释代码.这段代码的作用是它允许我在数据库中存储不同的URL,但是存储在那里的所有url都可以调用相同的页面,即使它们不同.我想一个确切的例子就是改变wordpress上的slu ..
会发生什么是businessbuilder类设置为接受对服务器的所有请求.当它遇到businessbuilder类时,它将访问数据库,找出你正在使用的子url,找到用户正在寻找的真实控制器,并访问该控制器.
经过大量的搜索后,我想我有一个解决方法.问题是我对实例的看法.在深入了解框架之后,我意识到它将实例存储为静态var,private static $ instance.如果已经填充了var,我修改了构造函数以不覆盖.最重要的是,由于加载时仍有一些奇怪之处,出于某种原因,对象会被标记为已加载,但实际上并非如此,我必须向控制器添加一个新的var,保护$ ci_instance.最后,我将CI_Controller修改为如下所示:
<?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 */
Run Code Online (Sandbox Code Playgroud)
到目前为止唯一的问题是我不能做$ this-> load-> model("some_model");. 相反,我必须使用$ this-> ci_instance-> load-> model("some_model"); 一切都将来自那里.我并不真正关心额外的var,但我不喜欢的是修改开箱即用的解决方案,因为它增加了升级的复杂性.
现在我已将此标记为答案,因为它是"我"选择用作我的解决方案,但我仍然打开了比我正在使用的解决方案更好的解决方案.需要解决的问题的确切描述如下:
将所有已加载的属性从一个实例复制到另一个 如果可能的话,基本上要合并两个实例.
如果有人能用比我更好的解决方案来解决这个问题,最好不修改codeigniter核心,我很乐意改变我的答案,因为我对我的解决方案不满意,因为我不知道我在开发过程中可能会遇到什么样的影响.