Codeigniter Call Controller From Controller -
after last 2 comments, i'll dump out real code , maybe out:
here landing controller:
<?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 necessary libraries $this->load->model("site_pages"); $rtr = $globals["rtr"]; // current site $site = current_site(); // requesting url $class = $rtr->uri->rsegments[1]; $function = $rtr->uri->rsegments[2]; // current function , class $current_method = explode("::", __method__); // real class name going 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 class name called $class = $site_page->class_name; } // execute if requested url not 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 required file. use require once incase file i've included require_once(apppath.'controllers/'.$rtr->fetch_directory().$class.ext); // create instance of class $ci = new $class(); if(method_exists($ci, $function)) // call method call_user_func_array(array(&$ci, $function), array_slice($rtr->uri->rsegments, 2)); else { show_404($rtr->fetch_directory().$class); exit; } } } }
here example of dynamic controller called:
<?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); } }
here model using:
<?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 * sites"); // failure on line, db undefined } }
the error getting either (error1):
a php error encountered severity: notice message: undefined property: public_homepage::$sites filename: controllers/public_homepage.php line number: 15 fatal error: call member function get() on non-object in /var/www/businessbuilderapp.com/public_html/application/controllers/public_homepage.php on line 15
or (error2):
a php error encountered severity: notice message: undefined property: businessbuilder::$db filename: core/model.php line number: 50 fatal error: call member function query() on non-object in /var/www/businessbuilderapp.com/public_html/application/models/bba_model.php on line 25
my theory why getting these errors because instance of object different 1 loaded model , libraries. what's odd though arrays carried over, not objects. in core loader.php of codeigniter array $_ci_models populated models not loaded in public_homepage class
also might first pass through businessbuilder class, able load , use modules successfully, when public_homepage called, that's when things start fail.
what makes confusing i'm trying figure out 2 errors 1 question mistake. here description of when errors:
error1:
when run code is, cannot call sites property.
error2:
when change call_user_func_array(array(&$ci, $function), array_slice($rtr->uri->rsegments, 2)); eval($class . "->" . $function);
i understand confusing, when explain it, if need more info, please let me know. note public_homepage looks because testing. there's no need dump more useless lines if error can produced minimal code.
update
after reading of answers, realized didn't explain code. code allows me store different urls inside database, urls stored there can call same page though different. guess exact example changing slug on wordpress.
what happens businessbuilder class set accept requests server. when hits businessbuilder class, access database, find out sub url using, find real controller user looking for, , access controller.
so after lots of searching, think have workaround. issue what thought instance. after diving framework realized storing instance static var, private static $instance. modified constructor not overwrite if var has been populated. on top of that, since there oddities still loading, reason objects marked loaded in reality not, had add new var controller, protected $ci_instance. in end, modified ci_controller following:
<?php if ( ! defined('basepath')) exit('no direct script access allowed'); /** * codeigniter * * open source application development framework 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 * * class object super class every library in * codeigniter 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 class objects instantiated // bootstrap file (codeigniter.php) local class variables // ci can run 1 big super object. foreach (is_loaded() $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 */
the issue far cannot $this->load->model("some_model");. instead have use $this->ci_instance->load->model("some_model"); , stem there. don't care var, don't modifying out of box solutions because increases complexity upgrade.
for i've marked answer because "i" have chosen use solution, still opened better solution 1 using. exact description of needs solved follows:
copy loaded properties 1 instance another. merger of 2 instances if possible.
if can answer better solution mine, preferably without modifying codeigniter core, i'd gladly change answer because not happy solution because don't know effects might encounter later on during development.
Comments
Post a Comment