Route CodeIgniter HMVC

lets say, you have codeigniter with hmvc stucture

in general hmvc you access the modules with url like this

http://your_domain.com/your_modules_controllers/your_function

or in my local development environment

http://localhost:8081/web/modules_controllers/function_controller

in my case, i want to develop Content Management System (CMS), with codeigniter and HMVC structure

every modules have admin controller to do Create Update Delete (CRUD) operation

the hmvc structure file

application
|__ controllers
|__ models
|__ views
..
|__ modules
     |__ content
     |    |__ controllers
     |    |    |__ admin.php 
     |    |    |__ content.php
     |
     |__ gallery
     |    |__ controllers
     |    |    |__ admin.php 
     |    |    |__ gallery.php
     |

..

admin page can access with url (example)

http://localhost:8081/project_web/user/logout
http://localhost:8081/project_web/user/login

how if we want simply it like

http://localhost:8081/project_web/logout
http://localhost:8081/project_web/login

we must configure the route


$route['logout']     = 'user/logout';
$route['login']     = 'user/login';

and this, i use function add_edit to input and update

http://localhost:8081/project_web/content/admin/add_edit 
http://localhost:8081/project_web/content/admin/add_edit/id_1

means: the content modules, admin controller, do add_edit (input operation), do add_edit/id_1 (update operation)

another i want, change the controllers admin first then the modules

http://localhost:8081/project_web/content/admin/
http://localhost:8081/project_web/gallery/admin/

to this

http://localhost:8081/project_web/admin/content/
http://localhost:8081/project_web/admin/gallery

its looks more ‘fancy’🙂

you need to route this again


$route['admin/([a-zA-Z_-]+)']            = '$1/admin/index';
$route['admin/([a-zA-Z_-]+)/add']        = '$1/admin/add_edit';
$route['admin/([a-zA-Z_-]+)/edit/(:any)']    = '$1/admin/add_edit/$2';
$route['admin/([a-zA-Z_-]+)/(:any)']    = '$1/admin/$2';

code igniter logo
code igniter logo

Published by

G3n1k

just to remember what i had known :)

One thought on “Route CodeIgniter HMVC”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s