wahmal wahmal - 6 months ago 22
PHP Question

create custom library with extends CI 3 core

i have a

custom library
to check
ajax
request. But it's not working.

This 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 error display :
library ci error

Answer

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');
            }
        }