Reputation: 947
I have a custom library to check for an AJAX
request, but it's not working.
The following code is my custom library:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Basic {
// We'll use a constructor, as you can't directly call a function
// from a property definition.
public function __construct()
{
// Assign the CodeIgniter super-object
// $this->CI =& get_instance()
}
public function check_ajax(){
if (!$this->input->is_ajax_request()) {
exit('No direct script access allowed');
}
}
}
?>
and this is my controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends CI_Controller {
function __construct()
{
parent::__construct();
}
function index(){
$this->load->view('Vhome');
}
function article_list(){
$this->basic->check_ajax();
$res= array('status' => false,'noty'=>'Article is empty','res'=>array());
$this->load->model('mquery');
$articles=$this->mquery->read('articles','','','post_date DESC');
if(!empty($articles)){
$i=0;
foreach ($articles as $key => $value) {
$res['res'][$i]=$value->post_title;
$i++;
}
$res['status']=true;
}else{
}
die(json_encode($res));
}
}
?>
What's wrong?
This is the error message displayed:
Upvotes: 1
Views: 383
Reputation: 947
Thank Manmohan, iwas update my custom library be this :
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Basic {
protected $CI;
// We'll use a constructor, as you can't directly call a function
// from a property definition.
public function __construct()
{
// Assign the CodeIgniter super-object
// $this->CI =& get_instance()
$this->CI =& get_instance();
}
public function check_ajax(){
if (!$this->CI->input->is_ajax_request()) {
exit('No direct script access allowed');
}
}
}
?>
Upvotes: 1
Reputation: 740
You need to load the CI object in your library to access it objects with :-
$CI = & get_instance();
after this you can use the CI classes like into your library like :-
public function check_ajax(){
$CI = & get_instance();
if (!$CI->input->is_ajax_request()) {
exit('No direct script access allowed');
}
}
Upvotes: 3