Posted By

StevenW721 on 07/09/10


Tagged

codeigniter


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

Killswitch
educsis
fana


Codeigniter - One Controller to Rule Them All


 / 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.

  1. // Following goes in your default controller
  2. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  3.  
  4. class Home extends CI_Controller {
  5. var $_data = array ();
  6.  
  7. public function index()
  8. { // One Controller to rule them all!
  9. // Currently only goes three levels deep
  10. if ( $this->uri->segment(2) == false && $this->uri->segment(3) == false ) {
  11. // If there's no second and third segment in the URL then use the first segment to find the view
  12. $view = $this->uri->segment(1, 'home');
  13. } else if ( $this->uri->segment(3) == false ) {
  14. // If there's no third segment in the URL then use the first two segments to find the view
  15. $view = $this->uri->segment(1, '').'/'.$this->uri->segment(2, '');
  16. } else {
  17. // If there's is three segments in the URL then use all three to find the view
  18. $view = $this->uri->segment(1, '').'/'.$this->uri->segment(2, '').'/'.$this->uri->segment(3, '');
  19. }
  20.  
  21. if (!file_exists( APPPATH . 'views/' . $view . EXT )){
  22. $view = "404";
  23. header("HTTP/1.0 404 Not Found");
  24. }
  25.  
  26. $this->load->view( $view, $this->_data );
  27.  
  28. }
  29. }
  30.  
  31. /* End of file home.php */
  32. /* Location: ./application/controllers/home.php */
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39. //following goes in the system/application/config/routes.php after the default and scaffolding, replace home with your default controller name
  40. $route['(.*)'] = 'home';

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: wortell on September 21, 2010

two things... cool condensed lean controller [for starters]....

  1. 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...

  2. 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! :)

Posted By: wortell on September 21, 2010

two things... cool condensed lean controller [for starters]....

  1. 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...

  2. 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! :)

You need to login to post a comment.