Search code examples
codeigniterextendcodeigniter-restserver

Extends REST_Controller and MY_Controller at once in CodeIgniter


I have this situation where I need to access a global function and normally I will do that in MY_Controller for the normal CodeIgniter. But in this case, I am using a REST server and I noticed that the normal controller must extends REST_Controller to make it functioning.

From my AuthController

<?php defined('BASEPATH') OR exit('No direct script access allowed');
require APPPATH . 'libraries/REST_Controller.php';
class AuthController extends REST_Controller {}

REST_Controller

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
abstract class REST_Controller extends CI_Controller {}

I want to use MY_Controller to create a global function that can be accessed by any controller. For example, from AuthController I want to extend it to MY_Controller, but by doing so I won't be able to use the REST_Controller.

Is there any way I can extend to MY_Controller and REST_Controller at once so that I can use both functions from these two controller?


Solution

  • From your Auth controller, extends MY_Controller, and in MY_Controller extends REST_Controller.

    It will be like

    AuthController

    <?php defined('BASEPATH') OR exit('No direct script access allowed');
    
    class AuthController extends MY_Controller {}
    

    MY_Controller

    <?php
    require(APPPATH.'/libraries/REST_Controller.php');
    
    class MY_Controller extends REST_Controller
    

    You can write your global functions in your MY_Controller.