hale
hale

Reputation: 171

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';

Upvotes: 3

Views: 1221

Answers (3)

Abdulla Nilam
Abdulla Nilam

Reputation: 38584

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>

Upvotes: 1

Kunal
Kunal

Reputation: 604

it seems that you are having trouble to write long url each and every time....

So,better way is to use routes for ease. For Ex: $route['login'] = "main/login";

Now if you use login directly in url it will call main class and it's login method

using routes you can prevent exposing your controller name and also for shorten URl

Upvotes: 0

umefarooq
umefarooq

Reputation: 4574

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.

Upvotes: 1

Related Questions