Return to Snippet

Revision: 28516
at June 12, 2012 14:53 by StevenW721


Updated Code
// Following goes in your default controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller {
	var $_data = array ();
	 
	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, '');
		}
		 
		if (!file_exists( APPPATH . 'views/' . $view . EXT )){
			$view = "404";
			header("HTTP/1.0 404 Not Found");
		}
		 
		$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';

Revision: 28515
at June 12, 2012 14:52 by StevenW721


Updated Code
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller {
	var $_data = array ();
	 
	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, '');
		}
		 
		if (!file_exists( APPPATH . 'views/' . $view . EXT )){
			$view = "404";
			header("HTTP/1.0 404 Not Found");
		}
		 
		$this->load->view( $view, $this->_data );
	 
	}
}

/* End of file home.php */
/* Location: ./application/controllers/home.php */

Revision: 28514
at September 11, 2010 03:23 by StevenW721


Updated Code
// Following goes in your default controller
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Home extends Controller {
	var $_data = array ();
	
	function Home()
	{	parent::Controller();		}
	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, '');	}
		
		if (!file_exists( APPPATH . 'views/' . $view . EXT ))
		{	$view = "404";
			header("HTTP/1.0 404 Not Found");	}
		
		$this->load->view( $view, $this->_data );
        
	}
}


//following goes in the system/application/config/routes.php after the default and scaffolding, replace home with your default controller name
$route['(.*)'] = 'home';

Revision: 28513
at September 10, 2010 13:39 by StevenW721


Updated Code
// Following goes in your default controller
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Home extends Controller {
	var $_data = array ();
	
	function Home()
	{	parent::Controller();		}
	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, '');	}
		
		if (!file_exists( APPPATH . 'views/' . $view . EXT ))
		{	$view = "404";	}
		
		$this->load->view( $view, $this->_data );
        
	}
}


//following goes in the system/application/config/routes.php after the default and scaffolding, replace home with your default controller name
$route['(.*)'] = 'home';

Revision: 28512
at September 10, 2010 13:39 by StevenW721


Updated Code
// Following goes in your default controller
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Home extends Controller {
	var $_data = array ();
	
	function Home()
	{	parent::Controller();		}
	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, '');	}
		
		if (!file_exists( APPPATH . 'views/' . $view . EXT ))
		{	$view = "404";	}
		
		$this->load->view( $view, $this->_data );
        
	}
}


//following goes in the system/application/config/routes.php after the default and scaffolding, replace home with your default controller name
$route['(.*)'] = 'home';
$route['(.*)'] = 'home';

Revision: 28511
at July 9, 2010 01:27 by StevenW721


Initial Code
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Home extends Controller {
	var $_data = array ();
	
	function Home()
	{	parent::Controller();		}
	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, '');	}
		
		if (!file_exists( APPPATH . 'views/' . $view . EXT ))
		{	$view = "404";	}
		
		$this->load->view( $view, $this->_data );
        
	}
}

Initial URL


Initial Description
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.

Initial Title
Codeigniter - One Controller to Rule Them All

Initial Tags
codeigniter

Initial Language
PHP