Codeigniter 3 autoload controller
Asked Answered
M

2

9

I'm using REST Server in codeigniter, and the way to use is that then in my app in all my controllers I must write this line on start:

require APPPATH . '/libraries/REST_Controller.php';

Does anyone know how to autoload this REST_Controller and to avoid this line in all my controllers? I don't want to use require.

Thank in advance

Murat answered 10/8, 2016 at 7:8 Comment(0)
N
6

You can achieve this through Codeigniter's autoload configuration.

Edit your project's autoload.php which is located in directory YourProject/application/config/

$autoload['libraries'] = array('REST_Controller');

And in controllers access this library class through $this->rest_controller.

BTW: Rest_Controllers is a library file, so I don't think a name suffixed with Controller is a good name for it.

Edit

Through your comment I got that you actually mean all of your controllers extended from REST_Controller, and you don't want require it at the top of every controller file.

Solution:

  1. Move REST_Controller.php into directory YourProject/application/core/.
  2. In YourProject/application/config/config.php line 119 change $config['subclass_prefix'] = 'MY_'; to $config['subclass_prefix'] = 'REST_';

then Codeigniter will load REST_Controller automatically.

But the subclass_prefix config has a global effect, and you need change the location of REST_Conttoller.php, so to make minimal change I think the best way is you create MY_Controller class in directory ./application/core/ and require REST_Controller at bottom of this new file. When CI load MY_controller automatically REST_Controller will be required too.

Notice: MY_Controller need extending from CI_Controller

Note answered 10/8, 2016 at 7:26 Comment(5)
All my controllers extend REST_Controller. So If I put in autoload in packages REST_Controller, I can't use $this->rest_controller, because I have to use before class started : class Users extends REST_Controller {}Murat
Ok, I know, I will edit my answer and provide solution for this.@MuratNote
I move REST_Controller in core folder and changed subclass_prefix, and working. With another solution I couldn't make app working, so I will keep first solution. Thanks.Murat
@Murat may be you forgot to change subclass_prefix's value to 'MY_' when you use second solution. Anyway, good to hear this answer works for you :)Note
No I haven't, I wrote MY_ for subclass_prefix and got that errorMurat
M
3

Put file include in constructor of MY_Controller class, then extend it to any controller that needs to use REST_Controller. If you don't have MY_Controller.php file in APPPATH.'core/' location, make one and use it as presented here:

<?php defined('BASEPATH') OR exit('See you next time.');

//APPPATH.'core/' location
class MY_Controller extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();

        require APPPATH . 'libraries/REST_Controller.php';//this constant ends with slash already
    }
}

Now, in every controller you want to use REST_Controller have code like this:

<?php defined('BASEPATH') OR exit('See you next time.');

//example controller
class Api extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    //bare example method
    public function some_get()
    {
        echo '<pre>', var_dump('Some REST_Controller code logic here'), '</pre>';
    }
}
Misapprehension answered 10/8, 2016 at 8:5 Comment(2)
I couldn't manage with this solution, got error 404 not found.Murat
I wrote that it is an example controller with example method. Update your question with how did you use it exactly so I could help more.Misapprehension

© 2022 - 2024 — McMap. All rights reserved.