drag2458 2015-07-31 00:40
浏览 64
已采纳

PHP的CodeIgniter访问控制器要求

I am using codeigniter for my project and I am stuck trying to figure this out.

I have some javascript that needs to perform an AJAX call to fetch some results based on a dropdown value that was selected.

function fetchLines(){
 $.ajax({
    url: baseURL + "resources/ajax.php?node=fetchLines",
    type: 'GET',
    cache: false,
    data: {
       lineType: 'business'
    },
    error: function(err) {
        alert(err.statusText);
    },
    success: function(data) {

        console.log(data);

    }

});
}

In this AJAX file, I am trying to include my controller and then access the function within it.

<?php
define('BASEPATH', "AJAX");
require_once('../application/controllers/Project.php');


switch($_REQUEST['node']){

    case 'fetchLines':
        $objLines = new Project();
        $objLines->fetchLines($_REQUEST['lineType']);
    break;

}

?>

My CI Controller then has a private function in it which I am trying to call to get the data I need:

private function fetchLines($lineType){
    $lines = $this->project_model->fetchLines($lineType);
    return $lines;
}

My goal here is to have an AJAX file or controller (if needed) be used for all my AJAX calls. It needs to be able to access a controller and return data.

With the current code above, I am getting the error: Class 'CI_Controller' not found in <b>C:\xampp\htdocs\blueprint\application\controllers \Project.php

Is there a better way to handle situations like this? I'm not an expert with OOP but some reading suggested something along these lines.

  • 写回答

3条回答 默认 最新

  • dongzhuo0895 2015-07-31 05:43
    关注

    why you are not sending this request to the controller method instead ?

            function fetchLines(){
             $.ajax({
                url: baseURL + "controller-name/method-name",
                type: 'GET',
                cache: false,
                data: {lineType: 'business'},
                error: function(err) {
                    alert(err.statusText);
                },
                success: function(data) {
    
                    console.log(data);
    
                }
    
            });
            }
    

    NOTE and in controller you can access these values as

    function method-name(){
        echo $this->input->get('lineType');
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?