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
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.
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 (.
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.
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.
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:
REST_Controller.php
into directory YourProject/application/core/
.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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With