/ 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
Copy this code and paste it in your HTML
// 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';