AjaxResponse Trait for CodeIgniter 2

Usage:

require_once APPPATH.'traits/AjaxResponse.php';

class CustomerList extends CI_Controller
{
    use AjaxResponse;

    public function __construct(){
    }
}

Trait

trait AjaxResponse
{
    protected $is_laravel = false;

    protected function json_response($data){
        if($this->is_laravel == false){
            header('Content-Type: application/json');
            echo json_encode($data);
            exit();
        }else{
            return response()->json($data);
        }
    }

    protected function success_response($data, $extra=null){
        $response = [
            'status' => 'success', 
            'data' => $data, 
            'extra' => $extra
        ];
 
        if($this->is_laravel == false){
            header('Content-Type: application/json');
            echo json_encode($response);
            exit();
        }else{
            return response()->json($response);
        }
    }
 
    protected function fail_response($msg, $code=0){
        $response = [
            'status' => 'fail', 
            'code' => $code, 
            'message' => $msg
        ];
 
        if($this->is_laravel == false){
            header('Content-Type: application/json');
            echo json_encode($response);
            exit();
        }else{
            return response()->json($response);
        }
    }
 
    protected function error_response($errors, $msg='網路錯誤'){
        $response = [
            'message' => $msg,
            'errors' => $errors
        ];

        if($this->is_laravel == false){
            header('Content-Type: application/json');
            echo json_encode($response);
            exit();
        }else{
            return response()->json($response, 422);
        }
    }
 
    protected function datatable_response($total, $filtered_total, $data=[], $draw=0){
        $out = array();
        $out['draw'] = $draw;
        $out['recordsTotal'] = $total;
        $out['recordsFiltered'] = $filtered_total;
        $out['data'] = $data;

        if($this->is_laravel == false){
            header('Content-Type: application/json');
            echo json_encode($out);
            exit();
        }else{
            return response()->json($out);
        }
    }
}