1

I am working on creating an application with having an HMVC structure in codeigniter and i also require API Centric application , so i used https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc for HMVC and Rest_Controller of Phil Sturgeon, now everything is working fine , but the problem is for each controller in the module i have to add the static path

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

i want to make this path dynamic , or auto load libraries for each module in the module's library folder, adding library name in the module/config/autoload.php is not working for me , as i think the reason is the code flow of application, first the application flow goes to the controller and it gets to my class extending the REST_Controller and gives the error , unable to load the REST_Controller Class

Note: if i put the Rest_Controller.php and Format.php in the application/libraries/ folder, everything works fine , but i want this to be modular , and make the module restful

how can i load libraries specifically from the module/libraries how to make it proper modular, so that there should be a complete instance of codeigniter in the module ,it should work freely, including the files loaded from the main application.

My folder structure is as follows

application
- - modules
- - - - modulename
- - - - - - controller
- - - - - - libraries
- - - - - - config
- - - - - - models
- - - - - - views

My Rest_Controller Class in the application/modules/module_name/libraries/Rest_Controller.php

abstract class REST_Controller extends MX_Controller

My Controller Class in application/modules/module_name/controllers/User.php

require APPPATH.'modules/modulename/libraries/REST_Controller.php';
class User extends REST_Controller
{

this works fine but if i remove the require line from the top , it doesn't work and even if i add the library name to autoload.php in the /modulename/config/autoload.php

it doesn't work My autoload.php /application/modules/modulename/config/autoload.php

<?phpif ( ! defined('BASEPATH')) exit('No direct script access allowed');
$autoload['packages'] = array();
$autoload['libraries'] = array('database', 'session','modulename/REST_Controller');
$autoload['helper'] = array('url', 'file');
$autoload['config'] = array();
$autoload['language'] = array();
$autoload['model'] = array();

and autoload.php in my application/config/autoload.php is empty, means no autoload mentioned

King Khan
  • 49
  • 1
  • 11
  • try use `autoload` in config.php or load in custom `MY_Controller` extended class – Bora Jun 17 '14 at 08:49
  • Bora , by autoload you mean $autoload and can you please more elaborate what you want me to try, i am new to codeigniter, thanks – King Khan Jun 17 '14 at 08:51
  • Something like that `$autoload['libraries'] = array('api/REST_Controller');` in `application/config/autoload.php`. HMVC extension should work properly. – Bora Jun 17 '14 at 08:57
  • on the main appliation level , i don't want to do that, i want the library to be loaded on the module level, and i have tried doing it in application/modules/modulename/config/autoload.php and it didn't worked – King Khan Jun 17 '14 at 09:04
  • If you want to load module level, api doesnt work while running another module. Module autoload run only user on api module. Could you share us your `autoload.php`? – Bora Jun 17 '14 at 09:10
  • yeah, i want it to load only for this module, or if i place it on other module2/libraries , then it should be loaded for module2. what i wanna get is that every module should be able to load it's libraries from it's specific libraries folder. I tried putting `$autoload['libraries'] = array('database', 'session','modulename/rest_controller'); ` didn't worked, getting **error
    Fatal error: Class 'REST_Controller' not found in /application/modules/modulename/controllers/User.php**
    – King Khan Jun 17 '14 at 09:17
  • Are you sure that HMVC extension works properly? Module locations etc.. Try main autoload `$autoload['libraries'] = array('modulename/REST_Controller');` in `application/config/autoload.php` for test working or not. – Bora Jun 17 '14 at 09:28
  • tried that, putting mentioned in the main autoload.php in application/config/autoload.php , still not working, same error and also the modular thing is working , as the error mentions that it's reaching to the controller file with the modular api but not able to get the rest_controller class which is in the libraries folder of the module – King Khan Jun 17 '14 at 09:31
  • Even though you write in there and does not work, there is a problem about HMVC. There are high chances it will work when you move `REST_Controller.php` to `application/libraries` and autoload without prefix `modulename` – Bora Jun 17 '14 at 09:38
  • yeah, that way it works , and that's i don't want , because it will then load for the complete application, not for the module , and this way the modularity of the application is disturbed – King Khan Jun 17 '14 at 10:13
  • Check out again HMVC files and configuration.. – Bora Jun 17 '14 at 10:18
  • don't know what to check , because hmvc is working fine, controllers are working, urls are fine , it's taking views from proper folder and also controllers can be loaded into other controller – King Khan Jun 17 '14 at 11:13
  • Hmm.. Did you try running the constructor like that? `class User extends REST_Controller{ $autoload = array('libraries' => array('modulename/REST_Controller')); }` – Bora Jun 17 '14 at 11:18
  • yeah, i have tried, but the logic is that when we are writing `class User extends REST_Controller{` the same time it looks for the rest_controller to extend and compiler is not able to find the class it gives error , and the autload line is executed after the declaration of class – King Khan Jun 17 '14 at 11:31
  • somehow the CI should load the configurations and the libraries before the instantiation of the constructor classes , but i think so CI is first loading the constructor class that's y it start looking for the REST_Controller class to extend and unable to find that, i also tried to echo something in the /application/modules/modulename/config/autoload.php , it doesn't echo anything before the class instantiation , and echoes afterwards – King Khan Jun 17 '14 at 11:35

1 Answers1

0

First - it isn't clear but your structure should look like this:

application
- - modules
- - - - modulename
- - - - - - controller
- - - - - - - modulename.php
- - - - - - libraries
- - - - - - - Rest_Conctroller.php
- - - - - - config
- - - - - - - autoload.php
- - - - - - models
- - - - - - views

Then use

$this->load->library('rest_controller');

Within your modulename.php controller to check it works first of all and you have all the paths defined correctly.

If it works then comment that line out first of all.

Then within your autoload.php file in your modulename/config folder just set:

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

Then try it again - notice there is nothing before the class name.

This will definitely work as I have tried it - if it doesn't it seems you have either modified your routes incorrectly or have been playing with files elsewhere (maybe over-ridden something in a custom controller?)

GrahamTheDev
  • 22,724
  • 2
  • 32
  • 64
  • As a side-note when testing things like this create a file called 'working.php' with class 'Working' -> then in `__construct` just echo "I am working" -> it is an easier way to isolate the problem. – GrahamTheDev Jun 21 '14 at 12:34
  • Thanks for your reply, but i have the same structure as you have mentioned above and it doesn't work, i have tried both ways told by you, doesn't work, and i have no routes defined, the routes.php is empty, and i have other problems also, like if i put subdirectories in views directory of module it don't load view from the subdir only from the main directory, i was using CI version 2.2., i have downloaded 2.1.4, but still doesn't work – King Khan Jun 22 '14 at 08:34
  • Sorry but without actually debugging your code directly I don't think I can help then. I have used it for quite some time without a hitch - sounds like you are either trying to auto-load a module twice, which we cause it to fail, or that you have something wrong in your config file. Also check your .htaccess file - Did you try the 'working.php' file way of doing things - it does at least rule out your rest controller being the cause. – GrahamTheDev Jun 22 '14 at 18:37
  • i have a MY_Controller which extends MX_Controllers can i just extend MY_Controller to REST_Controller? – kev_m Apr 19 '16 at 11:16