Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Separating Controllers for each module in Code Igniter

P.S This question may already be existing but the answer didn't satisfied what I was trying to figure out. I'm using code igniter for the first time in my new project.

So I have this only one controller which is main.php and populated with a lot of public function. Now everytime I go to main.php the url is looks like cmms/main and everytime I'm going for its subclass it goes cmms/main/asset.

Now the subclass asset has many functions which are in main.php. What I want is to make separate controllers for each module. so i have cmms/main as main.php & cmms/asset as asset.php instead of making it under cmms/main/asset. Would this be possible? or I should just leave it alone and continue putting all codes in the main.php controller? My default route is the main controller.

$route['default_controller'] = 'main';
like image 892
hale Avatar asked Nov 27 '25 13:11

hale


2 Answers

You have two ways to do that.

  1. Keep one controller but URL is different. (Method 01)
  2. Keep new controller for each new function. (Method 02)

(these topics describe below)


Method 01

go to - config/routes.php, and add new routes like this

$route['cmms/asset'] = 'cmms/main/asset';
$route['cmms/contact'] = 'cmms/main/contact';

So in view you should call anchor tags like this

<a href= "<?php echo base_url() ?>cmms/asset">Assets</a>
<a href= "<?php echo base_url() ?>cmms/contact">Contact Us</a>

Method 02

Create new controller for each new methods.

File name - main.php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Main extends CI_Controller {

}

File name - asset.php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Asset extends CI_Controller {

}

File name - contact.php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Contact extends CI_Controller {

}

So in view you should call anchor tags like this

<a href= "<?php echo base_url() ?>asset">Assets</a>
<a href= "<?php echo base_url() ?>contact">Contact Us</a>
like image 123
Abdulla Nilam Avatar answered Nov 29 '25 02:11

Abdulla Nilam


you can create Modules for your class in following ways,

  1. By creating separate class under your application controllers directory eg: main.php, assets.php here your URL will be cmms/main or cmms/asset
  2. Creating sub folders under your application controllers directory and create controller classes under it main/main.php, asset/asset.php here URL will be cmms/main/main, cmms/asset/asset, if you want just cmms/main or cmms/asset in URL than you need to create routes in route.php file.
  3. This way you can make codeigniter Modular in this way every module will have separate controller, model and view even libraries in its own module directory and URL will be same as cmms/main or cmms/asset for each controller. you need to install wiredesignz's HMVC extension here is URL you can download https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/overview

important please read documentation for HMVC extension to use it.

like image 32
umefarooq Avatar answered Nov 29 '25 03:11

umefarooq



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!