在Codeigniter 2.0中扩展控制器

时间:2017-04-18 04:46:14

标签: php codeigniter

我想问一下,如果在codeigniter中扩展控制器的正确方法是什么。因为我现在遇到Fatal error: Class 'ApiController' not found in两个文件都驻留在同一目录api文件夹中。这是我的代码:

<?php
//EchelonApiController.php
class EchelonApiController extends ApiController {

    public function __construct() {
       parent::__construct();
    }

    public function index(){
        echo 'asd';
    }
}


<?php
// ApiController.php
class ApiController extends CI_Controller {

    public $table;

    public function __construct(){

        parent::__construct();

        $this->load->models("api/".$table);
    }

    public function index(){

    }
}

4 个答案:

答案 0 :(得分:1)

在扩展之前,您需要包含基类文件。

<?php
//EchelonApiController.php

require "path/to/file/ApiController.php";

class EchelonApiController extends ApiController {

    public function __construct() {
       parent::__construct();
    }

    public function index(){
       echo 'asd';
    }
}

如果 ApiController.php EchelonApiController.php 都在同一个文件中,可以直接使用

require "ApiController.php";

否则只需在 APPPATH 常量的帮助下添加正确的路径。

答案 1 :(得分:0)

也许这可能对你有所帮助。 Extending The Controller Class in CodeIgniter

这似乎是类似的情况,也许文件放置是您问题的一部分。

答案 2 :(得分:0)

将您的ApiController放在application / core目录中。

答案 3 :(得分:0)

类欢迎扩展CI_Controller {

function __construct()
{
    parent::__construct();
    $this->load->database();
    $this->load->model("User_model");
    $this->load->helper("form");
    $this->load->helper("url");
}
/**
 * Index Page for this controller.
 *
 * Maps to the following URL
 *      http://example.com/index.php/welcome
 *  - or -
 *      http://example.com/index.php/welcome/index
 *  - or -
 * Since this controller is set as the default controller in
 * config/routes.php, it's displayed at http://example.com/
 *
 * So any other public methods not prefixed with an underscore will
 * map to /index.php/welcome/<method_name>
 * @see https://codeigniter.com/user_guide/general/urls.html
 */
function do_upload($name){
            $this->load->helper("form");

$config['upload_path'] = 'upload/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width']  = '102400';
$config['max_height']  = '76800';
$this->load->library('upload', $config);

$this->upload->initialize($config);
if (!$this->upload->do_upload($name)) {


}else{
$databasea['upload_data'] = $this->upload->data();
$this->load->library('image_lib');
return $databasea['upload_data']['file_name'];
}

}
public function index()
{
    if(isset($_POST['resgisterd'])){

        $insert['fname'] = $_POST['fname'];
        $insert['lname'] = $_POST['lname'];
        $insert['address'] = $_POST['address'];
        $insert['country'] = $_POST['country'];
        $insert['state'] = $_POST['state'];
        $insert['city'] = $_POST['city'];
        $insert['tittle'] = $_POST['tittle'];
        $insert['company'] = $_POST['company'];
        $insert['phone'] = $_POST['phone'];
        $insert['email'] = $_POST['email'];
        $insert['password'] = $_POST['password'];

        $insert['image'] = $this->do_upload('image');

        $this->User_model->insertrow($insert,'candidate');

    }



    $data['country'] = $this->User_model->fetchrow('country');
    $this->load->view('front/reg',$data);
}

function getstate($id){

     $whr['country_id'] = $id;
     $state = $this->User_model->fetchrowedit('state',$whr);
    $option = '';
    foreach ($state as $states ) {
    $option = '<option>Select State</option>';

    $option .= '<option value="'.$states->state_id.'">'.$states->state_name.'</option>';

    }

    print_r($option);

}

    function getcity($sid){

        $whr['state_id'] = $sid;
        $city12 = $this->User_model->fetchrowedit('city',$whr);
        echo $this->db->last_query();
            foreach ($city12 as $city123 ) {
        $option1 = '';
        $option1 .= '<option value="'.$city123->city_id.'">'.$city123->city_name.'</option>';



            }
            print_r($option1);
    }


    function checkremote(){

        $uName['email'] = $this->input->post('email');
        $isUNameCount = $this->User_model->alreadyexits('candidate',$uName);
        if($isUNameCount > 0){
                echo 'false';
        }else{

             echo 'true';

            }
        }



function login(){

    if(isset($_POST['login'])){

        $log['email'] = $_POST['email'];
        $log['password'] = $_POST['password'];
        $details = $this->User_model->fetchrowlogin($log,'candidate');

        if(count($details)){
            $ids = $details['id'];
            $email = $details['email'];
            $fname = $details['fname'];

            $this->session->set_userdata(array(

                'custid' => $ids,
                'emailid'=> $email,
                'fname'=> $fname,
            ));
        redirect('http://localhost/test27/index.php/welcome/dashboard');
        }else{

        redirect(base_url().'front1');


        }

    }



    $this->load->view('front/login');
}

function dashboard(){

$id = $this->session->userdata('custid');   
$whr['id']=$id;

if(isset($_POST['resgisterd'])){

        $insert['fname'] = $_POST['fname'];
        $insert['lname'] = $_POST['lname'];
        $insert['address'] = $_POST['address'];
        $insert['country'] = $_POST['country'];
        $insert['state'] = $_POST['state'];
        $insert['city'] = $_POST['city'];
        $insert['tittle'] = $_POST['tittle'];
        $insert['company'] = $_POST['company'];
        $insert['phone'] = $_POST['phone'];

        if(!empty($_FILES['image']['name'] && isset($_FILES['image']['name']))){
        $insert['image'] = $this->do_upload('image');

    }else{

                    $insert['image'] = $_POST['preimage'];

    }
            $whrs['id']=$id;

        $this->User_model->updaterow($insert,'candidate',$whrs);

        echo $this->db->last_query();
 redirect('http://localhost/test27/index.php/welcome/dashboard');

    }
    $data['info'] = $this->User_model->fetchrowedit('candidate',$whr);
    $data['country'] = $this->User_model->fetchrow('country');
    $this->load->view('front/dashboard',$data);

}

function message_box(){

$id = $this->session->userdata('custid');

$data['message_info']= $this->User_model->messages_list($id);


$this->load->view('front/message_box',$data);

}

function perticular($id){

$id1 = $this->session->userdata('custid');

$data['message_info']= $this->User_model->messages_perticular($id,$id1);
echo $this->db->last_query();

$this->load->view('front/message_box',$data);

}

function login(){

        if(isset($_POST['login'])){

            $login['email'] = $_POST['email'];
            $login['password'] = $_POST['password'];

            $
        }


}


function logout(){

    $this->session->unset_userdata('custid');
    $this->session->unset_userdata('emailid');
    $this->session->unset_userdata('fname');

}

}