Library not being extended in codeigniter - php

So I'm trying to extend the input library (CI 2.1.1), and when I call my custom save query function, its saying the function doesn't exist.
File: MY_Input.php, in the applications/libraries folder:_
class MY_Input extends CI_Input {
var $CI;
function __construct() {
parent::__construct();
$this->CI =& get_instance();
}
function save_query($query_array) {
$this->CI->db->insert('ci_query', array('query_string' => http_build_query($query_array)));
}
}
And in the controller I'm calling the function like this
$query_id = $this->input->save_query($query_array);
So what on earth am I doing wrong that it's giving me this error:_
Fatal error: Call to undefined method CI_Input::save_query() in ....
Can't see why it's not working, I even checked the user guide and according to it I guess I'm doing it right. :/

The CI_Input class is a core library (new thing in CI2.0.0). You will have to put your MY_Input.php file under application/core/ to make the framework pick it up.
When in doubt, look for the original class under system/core or system/libraries and mirror it under application/.

Related

Issue loading language file, CI

I have extended CI's baked in form validation, however I'm having issues loading a language files. I have the following code:
class MY_Form_validation extends CI_Form_validation {
public function __construct() {
parent :: __construct();
$this->load->lang('raf');
}
However this throws the error:
Call to a member function lang() on a non-object
Can anyone spot the issue here?
To load language in codeigniter, Syntax is :
$this->lang->load('filename', 'language');
Not:
$this->load->lang();
Try after load Helper file
$this->load->helper('language');

Changed controller load locations, and can't use any functions in controllers

By default, as many of you probably know, codeigniter loads controllers from application/controllers (URI Segment 0). Well... I changed it so that it loads them fom application/modules/(URI Segment 0)/controllers/(URI Segment 1 / not set: URI Segment 0). Up to here it all works fine, but when I create a controller, I cannot use any of the default function as $this->load or etc... Example:
/**
* Home Controller Class
*/
class Home extends CI_Controller
{
function __construct()
{
$this->load->database();
}
public function index()
{
echo "Testing...";
}
}
This example would always result in an error:
Severity: Notice
Message: Undefined property: Home::$load
Filename: controllers/home.php
Line Number: 10
Well I don't know how you exactly changed the Controller location.
But when you extend the CI_Controller, and you use a constructor method, you need to execute the parent's __construct() method at first, as follows:
class Home extends CI_Controller
{
function __construct()
{
/* Execute the CI_Controller constructor */
parent::__construct();
$this->load->database();
}
}
That's because your local constructor will be overriding the one in the parent controller class. So you need to manually call it.
CodeIgniter Doc.
Similar topics on SO:
Undefined $load property error after upgrading CodeIgniter 1.7 to 2.1
Codeigniter will not load language

How to get rid of &get_instance() in Codeigniter

I have been using CI for two years now. One thing that really annoys me is the use of &get_instance(). Although it is halpful while we are inside library , helper , presenters , model etc. But everytime loading it is cumborsome. If you forget loading it somewhere and simply use $this->blah->blah() instead of $CI->blah->blah() this makes too much trouble and if you are working online you face the client who is complaining that he sees the error. I have seen in the laravel that you does not need to load the instance anywhere throughout the application. This is because laravel is autoloading all the libraries and models and both are available anywhere in the application. But this seems to me disadvantage why loading classes that are not required in some particular places. This tells me Codeigniter is flexible but still i want an alternative where i dont want to use &get_instance(). Any idea or suggestion ? Please.
In your model or Core model or library
//class MY_Model extends CI_Model
//class SomeLibrary
class Some_model extends CI_Model {
private $_CI;
public function __construct() {
parent::__construct(); //for model or core model
$this->_CI =& get_instance();
}
//if you called attributs who does not exist in that class or parent class
public function __get($key)
{
return $this->_CI->$key;
}
//if you called methods who does not exist in that class or parent class
public function __call($method, $arguments)
{
call_user_func_array(array($this->_CI, $method), $arguments );
}
public function test() {
var_dump($this->some_controller_key);
var_dump($this->some_lib_loaded);
}
}
*NOT TESTED YET
Inspired by an piece of code from the awesome Flexi Auth
//from a Model to keep access of CI_Controller attributs
public function &__get($key)
{
$CI =& get_instance();
return $CI->$key;
}
I was shocked when I saw that ^^
To explain the &__get, i think when you will call this magic method a second time PHP will do not execute it again, but will take his result from the first call.

I'm trying to extend the CodeIgniter Form Validation library

This is my custom validation function. It uses geocoding from a Google Maps CodeIgniter library to check if a location exists.
public function address_check($str)
{
$this->load->library('GMap');
$this->gmap->GoogleMapAPI();
// this method checks the cache and returns the cached response if available
$geocodes = $this->gmap->getGeoCode("{$str}, United States");
$this->form_validation->set_message('address_check', 'The %s field contains an invalid address');
if (empty($geocodes))
{
return FALSE;
}
else
{
return TRUE;
}
}
If I place the function above inside my Controller along with the following rule, it works perfectly well.
$this->load->library('form_validation');
$this->form_validation->set_rules('location', 'Location', 'callback_address_check');
Now I simply want to move it out of my Controller. So I'm trying to extend my CodeIgniter Form Validation library as per this SO answer and the CI documentation.
I created a file here: /codeigniter/application/libraries/MY_Form_validation.php:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Form_validation extends CI_Form_validation {
public function __construct()
{
parent::__construct();
}
public function address_check($str)
{
$this->load->library('GMap');
$this->gmap->GoogleMapAPI();
// this method checks the cache and returns the cached response if available
$geocodes = $this->gmap->getGeoCode("{$str}, United States");
$this->form_validation->set_message('address_check', 'The %s field contains an invalid address');
if (empty($geocodes))
{
return FALSE;
}
else
{
return TRUE;
}
}
}
And from within my controller, I am setting the rule like this...
$this->load->library('form_validation');
$this->form_validation->set_rules('location', 'Location', 'address_check');
The first problem I found and solved myself was that nothing was happening because that SO answer incorrectly specified the file name to be My_Form_validation.php where it should have been MY_Form_validation.php
Now that the function is being called, the new problem is that I am getting the following error:
Message: Undefined property: MY_Form_validation::$load
Filename: libraries/MY_Form_validation.php
Line Number: 12
This is line 12:
$this->load->library('GMap');
I can't access a library from within a library? What's the proper way to fix this? I'd prefer not to auto-load the GMap library since I won't be using it all the time. Any other problems within my method?
Use this:
$CI =& get_instance();
$CI->load->library('GMap');
And then use it like:
$CI->gmap->GoogleMapAPI();
You have to do it that way, because Form Validation is not like CI Model or Controller class, it's only library.
Inside of your library, you can extend other models, libraries, configs, helpers, etc... by first loading the CI class. For example, in your constructor you can accomplish this through the following:
public function __construct()
{
parent::__construct();
$this->ci =& get_instance();
}
Once you have loaded the CI class, you can load any other classes that may need loading.
Examples:
$this->ci->load->library("session");
$this->ci->load->model("my_model");
$this->ci->load->helper("file");
Or in your case:
$this->ci->load->library("GMap");
You can then call functions from the class in a similar fashion throughout your class:
$this->ci->gmap->GoogleMapAPI();
$this->ci->gmap->getGeoCode("{$str}, United States");

CodeIgniter load class into class

i have start working with CodeIgniter, but i can't understand one think. How do i load one class into another?
$this->load->library("hello_world");
This is not working?
my class -> load -> hello_world class
class myclass {
function test() {
$this->load->library("hello_world");
$this->hello_world->hello();
}
}
Message: Undefined property: myclass::$load
The ability to load a class depends on the load->library function being available. It is made available to the controller and model classes, but extending these may not be appropriate for your use.
Instead you can either get a reference to CI and use that to load and refer to your class, or you can load it as usual in PHP ($c = new MyClass).
To get a rerence to CI use the following:
$CI =& get_instance();
$CI->load->helper('url');
$CI->load->library('session');
$CI->config->item('base_url');
etc.
You have to extend the CI controller/model
e.g.
class Some_controller extends Controller
{
public function index() {}
}

Categories