Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter 3 autoload controller

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

like image 428
Sasa Avatar asked Aug 10 '16 07:08

Sasa


People also ask

How to autoload Controller in CodeIgniter?

Open the application/config/autoload.Go to the array of the section where you want to add your file. I mean if you want to add a library file i.e. a file in the library folder of application folder, you need to go to the array of library section in the autoload. php file.

How to autoload library in CodeIgniter 3?

To autoload resources, open the application/config/autoload. php file and add the item you want loaded to the autoload array. You'll find instructions in that file corresponding to each type of item. Do not include the file extension (.

How to autoload model in CodeIgniter?

Auto-loading Models If you find that you need a particular model globally throughout your application, you can tell CodeIgniter to auto-load it during system initialization. This is done by opening the application/config/autoload. php file and adding the model to the autoload array.

What is the use of autoload in CodeIgniter?

CodeIgniter comes with an "Auto-load" feature that permits libraries, helpers, and models to be initialized automatically every time the system runs. If you need certain resources globally throughout your application you should consider auto-loading them for convenience.


1 Answers

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

like image 200
Kevin Yan Avatar answered Oct 08 '22 09:10

Kevin Yan