I want to check if user is logged in CodeIgniter by using my library in the controller's constructor.
This is my library:
class Administrator_libs {
public function validate_authen(){
if( $this->session->userdata('user_authen') ){
redirect(base_url().'admin/login/');
}
}
}
And this is my controller:
class Administrator extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->library('administrator_libs');
$this->administrator_libs->validate_authen();
$this->load->model('mod_menu');
}
}
But I get this error message:
Undefined property: Administrator_libs::$session
How can I use session in a library in CodeIgniter?
If you want to access any CodeIgniter library inside of your own, you must call get_instance(). This is because $this is bound to your current library and not the CodeIgniter object.
$CI =& get_instance();
if( $CI->session->userdata('user_authen') ){
redirect(base_url().'admin/login/');
}
Please see Creating Libraries CodeIgniter Documentation. Specifically the content under Utilizing CodeIgniter Resources within Your Library
This assumes you autoload the session library in config/autoload.php, if not, you'll also need to add $CI->load->library("session"); after $CI instantiation.
IMPORTANT: =& is not a typo. It's passed by reference to save memory.
You should simply go to application/autoload.php and add your autoload package which should look like somewhat like this : $autoload['packages'] = array('database','form_validation','session','email');
you can see there is session package that i added in my packages. Now coming to your constructor you should load this package by adding this : $this->load->library("session");
Session and any other lib / helper , etc extends from CI_Controller / CI_Model / etc...
If you are trying to use $this->whatever on a library that doesn't extends from any of this CI modules, you'll get the error.
As Jordan says, you can use get_instance.
Related
I installed the Bed Edmund's Ion Auth login script and would like to make the functions global throughout my application.
I installed the code and it works great, but I'm having issues extending it. The ion Auth controller extends CI_Controller, so then in all my other controllers I extend Auth.
//Ion Auth Controller Class
class Auth extends CI_Controller {
and in a separate page:
//Home Page Controller Class, where I will be placing the login form
require_once("auth.php");
class Home extends Auth {
By doing this, I get the following PHP error:
Message: Assigning the return value of new by reference is deprecated
I read in the CodeIgniter docs that extended classes must now begin with MY_, so I tried that as well with no luck.
What am I doing wrong? Is my logic all wrong?
Do a CTRL-F for =& inside the Auth class source code (the one you installed) and replace every instance by =.
$foo =& new Bar();
is a deprecated syntax that was used in PHP4 but no longer in PHP5.
So for example, i want to access config on CI from my class library.
Class A {
funcion x() {
$this->config->load('my_config'); // accessing my_config
}
}
that obviously won't work unless you extends and then call parent::__construct().
As far as i know, it can only be done from classes that extend CI_Controller or CI_Model. How to access CI stuff (config, helper, model, etc) on non-CI class ?
Thanks.
How about initiating the class from the construct?
include ("CI_Page.php");
class A {
protected $_CIInstance1;
protected $_CIInstance2;
public function __construct(){
$this->_CIInstance1 = new xxxx();
$this->_CIInstance2 = new yyyy();
}
public function x(){
$this->_CIInstance1->load('my_config');
}
}
You probably want to access the instance of CI as if it were the super variable, $this. In reality what you need is the ability to access the same functionality as $this and in order to do this, you'll need to use $CI =& get_instance();
You can find it directly in the documentation for Creating Libraries
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");
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/.
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() {}
}