【发布时间】:2016-05-13 06:06:31
【问题描述】:
我有一个自定义库来检查 AJAX 请求,但它不起作用。
以下代码是我的自定义库:
<?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');
}
}
}
?>
这是我的控制器:
<?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));
}
}
?>
怎么了?
【问题讨论】:
-
在你的控制器
__construct方法中添加$this->load->library('basic'); -
感谢 4 您的评论,iam 在自动加载配置中加载库。
标签: php function codeigniter class