Php – Codeigniter HMVC to set up custom routes for modules

codeignitercodeigniter-hmvcPHP

I am working quite closely with Codeigniter the PHP framework:
http://www.codeigniter.com/

Now I've added this Modular Extensions – HMVC to my Codeigniter framework.
https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/overview

Now, I've created my own module in the modules folder and set up directories for controllers, models and views as instructed. However I'm struggling specifically with custom routing.

I have created the config directory in my module blog directory and I have created the routes.php file within.

Now to access my module in the browser i would go to localhost:8888/blog/ now I'm mostly asking out of curiosity, I wanted to create a custom route so that maybe I could access the page like localhost:8888/posts/ so I thought that setting up the following route would work:

$route['posts'] = 'blog';

or if I had a method called listings I could use

$route['posts/listings'] = 'blog/listings';

However this returns a 404 Page Not Found.

Is it possible to create custom routes like this in a module?

Best Answer

Setting up custom routes for HMVC Easy here are some examples below. You can use same techneique for CI3 Make sure you choose right version from here https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/downloads go to branches and choose your version The Default is for CI-2

$route['default_controller'] = 'catalog/common/welcome/index';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

// Common
$route['admin'] = "admin/common/login/index";
$route['admin/dashboard'] = "admin/common/dashboard/index";
$route['admin/logout'] = "admin/common/logout/index";
$route['admin/register'] = "admin/common/register/index";

// Users
$route['admin/users/user_edit/(:any)'] = "admin/user/users/user_edit/$1";
$route['admin/users/user_password/(:any)'] = "admin/user/users/user_password/$1";
$route['admin/users/user_key/(:any)'] = "admin/user/users/user_key/$1";

For Example:

admin will be module name.

application modules / admin <-- Admin Module Name

application / modules / admin / controllers / common <-- Sub folder
application / modules / admin / controllers / users <-- Sub folder

Please watch this great tutorial for beginners on HMVC https://www.youtube.com/watch?v=8fy8E_C5_qQ

You can also download Htaccess from here http://www.insiderclub.org/downloads you may need to join for free to download David's Insider Club suitable for codeigniter.

Related Topic