/ Published in: PHP
This one controller will load views based on the URI segments returned, good for static sites that don\\\\'t require a controller for every page. Requires a view \\\\"404\\\\" as a default when nothing is found.
Expand |
Embed | Plain Text
// Following goes in your default controller class Home extends CI_Controller { public function index() { // One Controller to rule them all! // Currently only goes three levels deep if ( $this->uri->segment(2) == false && $this->uri->segment(3) == false ) { // If there's no second and third segment in the URL then use the first segment to find the view $view = $this->uri->segment(1, 'home'); } else if ( $this->uri->segment(3) == false ) { // If there's no third segment in the URL then use the first two segments to find the view $view = $this->uri->segment(1, '').'/'.$this->uri->segment(2, ''); } else { // If there's is three segments in the URL then use all three to find the view $view = $this->uri->segment(1, '').'/'.$this->uri->segment(2, '').'/'.$this->uri->segment(3, ''); } $view = "404"; } $this->load->view( $view, $this->_data ); } } /* End of file home.php */ /* Location: ./application/controllers/home.php */ //following goes in the system/application/config/routes.php after the default and scaffolding, replace home with your default controller name $route['(.*)'] = 'home';
Comments
Subscribe to comments
You need to login to post a comment.

two things... cool condensed lean controller [for starters]....
with modular extensions, one can separate the views by modules, and that may work as a valid alternative, keeping less burden on the controller to create the view to use...
this could be a great 'helper' function - and called like
[code] $this->load->helper("findmyview"); // calling the application/helper/findmyview.php $this->get_view();// in this function you'd stick the the 'index()' function call above into. [/code]
excellent work! :)
two things... cool condensed lean controller [for starters]....
with modular extensions, one can separate the views by modules, and that may work as a valid alternative, keeping less burden on the controller to create the view to use...
this could be a great 'helper' function - and called like
[code] $this->load->helper("findmyview"); // calling the application/helper/findmyview.php $this->get_view();// in this function you'd stick the the 'index()' function call above into. [/code]
excellent work! :)