CodeIgniter Forums
Split controller - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Split controller (/showthread.php?tid=62870)



Split controller - maquejp - 09-03-2015

Hello,

I start working with codeigniter.
I have one Controller (called Pages default one) that contains all my functions.
I would like to split the file in several Controller to group by application section (House, Resident, Rentals, Utilities)
How can I achieve that?
I created server files at the same level that the main controller
....Controller
........Pages.php
........House.php
........Resident.php
........Rentals.php
........Utilities.php

It think that my main issue is about the url invoked by the views...
i.e. my navigation (views/templates/navigation.php) use the function name (logical) as href (<a href="rentals">...)
rentals being a function within Pages.php; if I move the rentals function within Rentals.php; I have of course a 404.

For information, my Routes.php contains the following
$route['default_controller'] = 'pages/index';
$route['(:any)'] = 'pages/$1';

I hope I am clear Smile


RE: Split controller - maquejp - 09-03-2015

I found a solution: custom libraries...
In my Page.php controller I have now:
...
public function houses() {
$this->load->library('HousesLib');
$this->houseslib->index();
}
...
And I have a library file HouseLib.php with this:
class HousesLib {
public function index() {
$CI = & get_instance();
$CI->load->library('session');
if ($CI->session->userdata('is_logged_in')) {
$CI->load->model('Houses');
......


RE: Split controller - mwhitney - 09-03-2015

PHP Code:
$route['(:any)'] = 'pages/$1'

This is basically sending everything to your pages controller, so you can't really use any other controllers without setting up routes for them above this one.