Extending CI_Controller / Indirect modification of overloaded property [duplicate] - codeigniter

This question already has answers here:
Call to a member function on a non-object [duplicate]
(8 answers)
Closed 10 years ago.
the CI community seems to have no answer to this (or maybe it is so obvious, that it looks like a noob question?), so I ask here.
I've been seeing this kind of error for a long time now (error messages follow), however I have always used this "dirty" workaround. Now I do not want to edit the core CI code anymore, and I know there must be a graceful solution to this.
Here is the setup.
In my application/config/routes I have this
$route['default_controller'] = "dispatcher";
The Dispatcher class in application/controllers/ is defined as this:
class Dispatcher extends MY_Controller {
function __construct() {
parent::__construct();
$this->load->helper(array('form','https'));
}
function index() {
# load models
$this->load->model('site/map');
$this->load->model('site/langs');
$this->load->model('site/pages');
if ( $mapped = $this->map->get_map() ){ // this is the line 21
$this->langs->set_user_lang( $this->GLO['show_lang_in_url'], $this->GLO['show_lang_at_home'] );
$red = base_url().$this->GLO['user_lang_label']."/".$mapped;
redirect($red, "refresh");
}
...
now MY_Controller resides in application/core and is defined as follows:
class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
$this->GLO = array();
#
# set defaults:
#
$this->GLO['deb'] = '';
$this->GLO['baseurl'] = base_url();
... (more GLO[key] = value )
}
public function __set ($key, $value ) {
$this->GLO[$key] = $value;
}
Every model I use starts like this (named accordingly):
class Map extends CI_Model {
function __construct(){
parent::__construct();
}
function get_map(){
.....
return true;
}
Now with CI 2.1.2, PHP 5.3.3, Apache 2 on Debian when trying to load a page, I am getting this error:
A PHP Error was encountered Severity: Notice Message: Indirect
modification of overloaded property Langs::$GLO has no effect
Filename: site/langs.php Line Number: 82
A PHP Error was encountered Severity: Notice Message: Undefined
property: Dispatcher::$map Filename: controllers/dispatcher.php Line
Number: 21
Fatal error: Call to a member function get_map() on a non-object in
/home/webdev/MC/_WWW/application/controllers/dispatcher.php on line 21
The line 21 is the one marked above in the Map model
if ( $mapped = $this->map->get_map() ){
the line 82 in the other model looks like this:
$this->GLO['langs'][] = $temp;
Throughout the code, this GLO array is referenced as $this-GLO[key]. It must be read and written from time to time. If I delete the __set function in MY_Controller, I get more of those warnings.
Where is the problem?
Thanks a lot in advance!

I'm not sure about the first error - but these two:
A PHP Error was encountered Severity: Notice Message: Undefined
property: Dispatcher::$map Filename: controllers/dispatcher.php Line
Number: 21
Fatal error: Call to a member function get_map() on a non-object in
/home/webdev/MC/_WWW/application/controllers/dispatcher.php on line 21
...look like you havent loaded your model correctly?
change
if ( $mapped = $this->map->get_map() ){
to
$this->load->model('map');
if ( $mapped = $this->map->get_map() ){

Related

configure flexigrid helper, and customize variable $colModel

I try to start Flexigrid into Codeigniter.
I download demo files from here http://gembelzillonmendonk.wordpress.com/2010/06/28/flexigrid-and-codeigniter-with-advanced-searching-with-example/
but i wrong to set up something:
Instruction from site:
extract .rar to CI folder**
open your controller (ie: CI_Folder\system\application\controllers\flexigrid.php)**
configure flexigrid helper, and customize variable $colModel, example:**
code example
What I did:
I installed correctly CodeIgniter (copy and paste inside htdocs - xampp)
I created a database called 'country' and import sql file inside demo.
I copied correctly files an folders from demo files to my CI folder (called grocery-crud-demo).
But when I type on firefox http://localhost/grocery-crud-demo/index.php/flexigrid/index I have this error
Fatal error: Class 'Controller' not found in C:\xampp\htdocs\grocery-crud-demo\application\controllers\flexigrid.php on line 2
A PHP Error was encountered
Severity: Error
Message: Class 'Controller' not found
Filename: controllers/flexigrid.php
Line Number: 2
Backtrace:
I read instruction also from here: http://roadmyapps.toile-libre.org/index.php/flexigrid/examplebut I don't understand which code exactly I must have into flexigrid.php
I don't understand how configure flexigrid helper, and customize variable $colModel.
I need exact code into files. Have you idea?
I find CodeIgniter 1.7.3 -- I repeat steps -- new error:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Ajax_model::$db
Filename: models/ajax_model.php
Line Number: 34
Fatal error: Call to a member function query() on a non-object in C:\xampp\htdocs\grocery-crud-demo\system\application\models\ajax_model.php on line 34
Code of ajax_model.php is this: http://pastebin.com/P3KawC7S
Probably you are using newer version of CI than one on tutorial.
As I can see tutorial is written for 1.7.2 version.
Try to change
class Someclass extends Controller
{
public function someclass()
{
}
public function index()
{
//etc, etc
}
}
with
class Someclass extends Controller
{
public function __construct()
{
parent::__construct()
{
}
}
public function index()
{
//etc, etc
}
}
Also, for models too. Like
public function Ajax_model()
{
parent::Model();
$this->CI =& get_instance();
}
should be
public function __construct()
{
parent::__construct();
$this->CI =& get_instance();
}
Try that way.
Spot those differences. Those should change in every controller and model respectivelly. Follow this link for upgrading your app.
If fail again, see the CodeIgniter documentation about upgrading versions.

Fatal error: Cannot use object of type stdClass - amfphp

I am newbee for Amfphp+codeignitor, I am unable to see the answer for the question amfphp 2.2, index.php gives fatal error?, Or it is not clear to me.
I followed the process mentioned here
http://www.nunomira.com/blog/2012/03/codeigniter-2-0-3-with-amfphp-2-0/
I am getting the same error - Fatal error: Cannot use object of type stdClass as array in D:\vhosts\site\application\libraries\Amfphp\Plugins\AmfphpMonitor\AmfphpMonitor.php on line 167
No Idea where i am doing the mistake.
http://localdomain.com/index.php/amf/gateway is the url i am trying.
How to rectify? what is the problem? if the problem is resolved, will i look service browser?
Experts please guide me on this...
Here is the code
Folder Structure
-controllers
-amf
-services
-Testservice.php
-Gateway.php
-libraries
-Amfphp (Amfphp folder)
Gateway.php
<?php
require_once APPPATH . "/libraries/Amfphp/ClassLoader.php";
class Gateway extends CI_Controller
{
function __construct()
{
parent::__construct();
}
function index()
{
$config = new Amfphp_Core_Config();//do something with config object here
$config->serviceFolders = array(dirname(__FILE__) . "/services/");
$gateway = Amfphp_Core_HttpRequestGatewayFactory::createGateway($config);
$gateway->service();
$gateway->output();
}
}
Testservice.php
<?php
class Testservice extends CI_Controller {
public function getMessage()
{
return array("param1" => "param1");
}
}
Thanks
someone pointed out a similar problem and a fix has been made but not released yet. Could you try replacing your AmfphpMonitor.php code with this one?
https://github.com/silexlabs/amfphp-2.0/blob/master/Amfphp/Plugins/AmfphpMonitor/AmfphpMonitor.php

CodeIgniter Undefined Variable while trying to create subview

Hi I am new to CodeIgniter and am taking a tutorial. I ran into an error
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: subview
Filename: admin/_layout_modal.php
Line Number: 7
An Error Was Encountered
Unable to load the requested file: .php
I followed the tutorial to the tee and I cant seem to find out where I am going wrong.
here is the controller
<?php
class User extends Admin_Controller {
public function __construct() {
parent::__construct();
}
public function login() {
$this->data['subview'] = 'admin/user/login';
$this->load->view('admin/_layout_modal', $this->data);
}
}
and the view
<?php $this->load->view($subview); ?>
Please help...
If you gave script from your .php file not tutorial then everything is ok.
You are probably typing the wrong url in when you try to access the page. Make sure you are loading "admin/user/login" instead of "admin/dashboard/modal".
If you are following tutplus tutorial- building cms with codeigniter, First watch two episodes, Managing User Part 1 and Managing User Part 2 and then Start building, Your question will be answered on Part 2.... the dashboard has to declare the subview variable and
CREATE THAT admin/dashboard/index.php file in views
class Dashboard extends Admin_Controller{
public function __construct(){
parent::__construct();
}
public function index(){
$this->data['subview'] = 'admin/dashboard/index';
$this->load->view('admin/_layout_main',$this->data);
}
public function Modal(){
$this->load->view('admin/_layout_modal',$this->data);
}
}
don't need to pass data to $this. Just pass the data to
$data['variable_name']=data;
pass this $data variable to view through load class.

CodeIgniter Loader extend causes error

I've came across something that drives me crazy. please help me :(
I'm try to extend the CI_Loader Core in-order to create global header and footer.
For some reason, the extending causes error.
The extend code:
class MY_Loader extends CI_Loader
{
function __construct()
{
// i need the codeigniter super-object
// in all the functions
$CI =& get_instance();
}
public function load_header()
{
echo 'header';
}
public function load_footer()
{
echo 'footer';
}
}
The error i currently get
Unable to load the requested file: helpers/url_helper.php
UPDATE: CHANGED THE CODE - NEW ERROR
class MY_Loader extends CI_Loader
{
function __construct() {
parent::__construct();
$CI =& get_instance();
$CI->load = $this;
}
public function load_header()
{
echo 'header';
}
public function load_footer()
{
echo 'footer';
}
}
now i get some error with the header
A PHP Error was encountered
Severity: Warning
Message: Cannot modify header information - headers already sent by (output started at /mounted-storage/home75b/sub007/sc43190-SREL/gogetmilk.com/application/core/MY_Loader.php:1)
Filename: libraries/Session.php
Line Number: 672
UPDATE #2
I'm not its the right way to fix it. but its working.
line 672 in libraries/Session.php sets a cookie
i've added a test to see if headers were already sent:
if (! headers_sent() )
{
setcookie(
$this->sess_cookie_name,
$cookie_data,
$expire,
$this->cookie_path,
$this->cookie_domain,
$this->cookie_secure
);
}
its not working ok.
i've created session before printing anything, after printing, within views, after loading views, etc..
does anyone believe this will cause any problems?
problem solved and was not related to codeigniter. see answer at the bottom.
The error i currently get Unable to load the requested file:
helpers/url_helper.php
You're getting this error because you didn't load URL helper. I don't see any call to any URL helper function so I assume the error is being thrown in another file ... Try adding
$this->load->helper('url');
in relevant file or just load it in the config/autoload.php
and, yes, use parent::__construct(); as answered by Broncha
Your construct should call the parent's construct
function __construct()
{
parent::__construct();
// i need the codeigniter super-object
// in all the functions
$CI =& get_instance();
}
Problem solved. it wasn't even a CodeIgniter Issue.
It was an invisble BOM at the begining of the file.
Used this application to remove this bytes: http://sourceforge.net/projects/bomremover/
Thanks everybody for your help :)

Codeigniter Call Controller From Controller

After the last two comments, I'll dump out my real code and maybe it will help out:
Here is the 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 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;
}
}
}
}
here is an example of a dynamic controller that will be 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 is my model that I am 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 * FROM sites"); // failure on this line, db undefined
}
}
the error that I am getting is either this (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
or this (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
My theory as to why I am getting these errors is because the instance of the object is different than the one that loaded the model and libraries. What's odd about that though is that arrays are carried over, but not objects. So in the core Loader.php of codeigniter array $_ci_models is populated with models that are not loaded in the Public_homepage class
Also what might help you is that from the first pass through the businessbuilder class, I am able to load and use the modules successfully, but when Public_homepage is called, that's when things start to fail.
What makes this confusing is that I'm trying to figure out 2 errors with one question which is probably my mistake. Here is a description of when I get the errors:
Error1:
When I run the code as is, I cannot call the sites property.
Error2:
When I change the
call_user_func_array(array(&$CI, $function), array_slice($RTR->uri->rsegments, 2));
to
eval($class . "->" . $function);
I understand that this is really confusing, especially when I explain it, but if you need more info, please let me know. Also note that the Public_homepage looks like that because I am testing. There's no need to dump more useless lines if the error can be produced with minimal code.
Update
After reading some of the answers, I realized that I didn't explain the code. What this code does is that it allows me to store different urls inside a database, but all the urls stored there can call the same page even though they are different. I guess an exact example would be changing the slug on wordpress.
What happens is that the businessbuilder class is set to accept ALL requests to the server. When it hits the businessbuilder class, it will access the database, find out what sub url you are using, find the real controller that the user is looking for, and access that controller.
So after lots of searching, I think I have a workaround. The issue what what I thought with the instance. After diving into the framework I realized that it is storing the instance into as static var, private static $instance. I modified the constructor to not overwrite if that var has been populated. On top of that, since there were some oddities still with the loading, for some reason objects would be marked as loaded but in reality were not, I had to add a new var to the controller, protected $ci_instance. In the end, I modified the CI_Controller to look like the following:
<?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 */
The only issue so far is that I cannot do $this->load->model("some_model");. Instead I have to use $this->ci_instance->load->model("some_model"); and everything will stem from there. I don't really care about the extra var, but what I don't like is modifying out of box solutions because it increases the complexity to do an upgrade.
for now I've marked this as an answer because it is what "I" have chosen to use as my solution, but I am still opened to a better solution than the one I am using. An exact description of what needs to be solved is as follows:
Copy all loaded properties from one instance to another. Basically do a merger of two instances if possible.
If someone can answer that with a better solution than mine, preferably without modifying the codeigniter core, I'd gladly change my answer because I am not happy with my solution because I don't know what effects I might encounter later on during development.
In your application/autoload.php set codeigniter to load database class.
$autoload['libraries'] = array('database', 'otherlibrary', 'otherlibrary2');
It must be all you need to solve your problem.
if u using HMVC just using
Class Models extends MX_Loader{
function getUser($username){
$sql="SELECT
*
FROM
user
WHERE username = ? "
return $this->db->query($sql,array($username))->row();
}
}

Resources