Question:
How do I extend a controller of an HMVC module and then call that extended class in Codeigniter’s URI Router?
Details:
Assuming the following typical Codeigniter file structure as it relates to HMVC:
/
/application
/application/modules
/application/modules/module-name
/application/modules/module-name/controllers
/application/modules/module-name/controllers/controller-name.php
/application/modules/module-name/models
/application/modules/module-name/models/model-name.php
/application/modules/module-name/views
/application/modules/module-name/views/view-name.php
/application/config
/application/config/routes.php
I have a class Events stored in /application/modules/events/controllers/events.php. I would like to extend it to Courses and Conferences which would provide specific functionality to those respective controllers. I would like to store those controllers in the same module folder (ie. /applications/modules/events/controllers/courses.php, etc.) I would like to route users that visit http://www.example.com/courses to the courses controller like so:
$route['courses'] = 'courses/getList';
I would also like to route users that visit http://www.example.com/conferences to the conferences controller like so:
$route['conferences'] = 'conferences/getList';
The Problem:
Unfortunately, I can’t seem to get this working because courses isn’t contained in it’s own module and therefore can’t be found by the router. It appears that the router is looking for the course class file at the following file path: /applications/modules/courses/controllers/courses.php. I have tried move that file to the aforementioned path, but I am then asked for the location of the Events controller class that Courses extends from.
A Possible Solution:
One possible solution might be to contain each of the controllers in a single controller file. However, I do not wish to do this because I like to store each class in it’s own individual file and grouped with it’s extended classes if at all possible. I am looking for a more elegant and altogether correct solution. Even if I were to attempt this technique of a single file, it would need to be contained in /applications/modules/courses/controllers/courses.php to be accessed. This leaves me out of luck when trying to later route to the conferences controller.
It seems that by adding
to the beginning of my extended classes (
coursesandconferences) I will be able to leave theEventsmodule alone.Unfortunately, I have to create separate modules for the
coursesandconferencescontrollers. Oh well, I guess it can’t be perfect.