Today , we are Going to Use Codeigniter URI Segments to rule all Methods with in the controller, means one Controller will load views based on the uri segments, This methods is best for static sites that don’t require a controller for every page, we can set a 404 view when nothing is found.
Currently this Controller will only goes three levels deep
Open application/controllers/routes.php
replace welcome with your default controller name
I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.
Currently this Controller will only goes three levels deep
<?php if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Welcome extends CI_Controller
{
var $_data = array();
public function __construct()
{
parent::__construct();
}
public function index()
{
if (!$this->uri->segment(1) && !$this->uri->segment(2)) {
$view = 'welcome';
}
if ($this->uri->segment(1) && !$this->uri->segment(2)) {
$view = $this->uri->segment(1, 'welcome');
}
if ($this->uri->segment(1) && $this->uri->segment(2)) {
$view = $this->uri->segment(1, 'welcome') . '/' . $this->uri->segment(2, '');
}
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 welcome.php */
/* Location: ./application/controllers/welcome.php */
Open application/controllers/routes.php
replace welcome with your default controller name
$route['(.*)'] = 'welcome';
I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.
Comments