Reputation: 2172
I am new to CodeIgniter, building website with 2 nested controller directories backend
and frontend
. All my pages are working fine except root. I am facing difficulty to set a default page when there is not path specified in url (only the website root).
I want to render home page if there is no path after website root. I put $route['default_controller'] = 'frontend/pages'
, but its not working.
My config/routes.php
is as follows:
$route['default_controller'] = 'frontend/pages';
$route['user'] = 'user/index';
$route['user/register']['GET'] = 'frontend/user/index';
$route['user/register']['POST'] = 'frontend/user/register_user';
$route['user'] = 'frontend/user/login_view';
$route['user/login'] = 'frontend/user/login_view';
$route['user/login_user'] = 'frontend/user/login_user';
$route['user/user_profile'] = 'frontend/user/user_profile';
$route['user/user_logout'] = 'frontend/user/user_logout';
$route['admin'] = 'backend/Admin_area/dashboard';
$route['admin/(index|dashboard)'] = 'backend/Admin_area/dashboard';
$route['admin/(:any)/(:any)/(:any)'] = 'backend/$1/$2/$3';
$route['admin/(:any)/(:any)'] = 'backend/$1/$2';
$route['admin/(:any)'] = 'backend/$1';
$route['/^$'] = 'frontend/pages/view/home';
$route['(:any)'] = 'frontend/pages/view/$1';
When I visit root/
is show me 404 error page
.
Pages
Controller code:
<?php
class Pages extends Frontend_Controller {
function __construct()
{
parent::__construct();
}
public function index()
{
$page = 'home';
$this->data['pagetitle'] = ucfirst($page);
$this->render('pages/'. $page);
}
public function view($page = 'home')
{
$this->data['pagetitle'] = ucfirst($page);
$this->render('pages/'. $page);
}
}
Upvotes: 1
Views: 3327
Reputation: 2634
You can override 404 default mechanism using:-
$route['404_override'] = 'your_custom_or_default_page';
And default controller as:-
$route['default_controller'] = 'your_custom_or_default_page';
Upvotes: -1
Reputation: 9717
Hope this will help you :
On CodeIgniter 3 It does not allow you to have a sub folder on $route['default_controller']
you will instead need to create a MY_Router
.php file like below.
You will need to create a MY_Router.php in
application > core > MY_Router.php
class MY_Router extends CI_Router {
protected function _set_default_controller() {
if (empty($this->default_controller)) {
show_error('Unable to determine what should be displayed. A default route has not been specified in the routing file.');
}
// Is the method being specified?
if (sscanf($this->default_controller, '%[^/]/%s', $class, $method) !== 2) {
$method = 'index';
}
// This is what I added, checks if the class is a directory
if( is_dir(APPPATH.'controllers/'.$class) ) {
// Set the class as the directory
$this->set_directory($class);
// $method is the class
$class = $method;
// Re check for slash if method has been set
if (sscanf($method, '%[^/]/%s', $class, $method) !== 2) {
$method = 'index';
}
}
if ( ! file_exists(APPPATH.'controllers/'.$this->directory.ucfirst($class).'.php')) {
// This will trigger 404 later
return;
}
$this->set_class($class);
$this->set_method($method);
// Assign routed segments, index starting from 1
$this->uri->rsegments = array(
1 => $class,
2 => $method
);
log_message('debug', 'No URI present. Default controller set.');
}
}
In route.php
$route['default_controller'] = 'frontend/pages';
Upvotes: 1
Reputation:
By default in codeigniter the default controller route can only be on the first level of controllers.
All other controller can in a sub folder but just not the default controller you want to have.
Correct
application
application > controllers
application > controllers > Pages.php // Your Default Controller
Not
application
application > controllers
application > controllers > frontend > Pages.php
To be able to have sub folders for default_controller route
Like
$route['default_controller'] = 'subfolder/controller/function';
How to use a sub folder in default controller route in CodeIgniter 3
You will need to create a application/core/MY_Router.php file
<?php
class MY_Router extends CI_Router {
protected function _set_default_controller() {
if (empty($this->default_controller)) {
show_error('Unable to determine what should be displayed. A default route has not been specified in the routing file.');
}
// Is the method being specified?
if (sscanf($this->default_controller, '%[^/]/%s', $class, $method) !== 2) {
$method = 'index';
}
// This is what I added, checks if the class is a directory
if( is_dir(APPPATH.'controllers/'.$class) ) {
// Set the class as the directory
$this->set_directory($class);
// $method is the class
$class = $method;
// Re check for slash if method has been set
if (sscanf($method, '%[^/]/%s', $class, $method) !== 2) {
$method = 'index';
}
}
if ( ! file_exists(APPPATH.'controllers/'.$this->directory.ucfirst($class).'.php')) {
// This will trigger 404 later
return;
}
$this->set_class($class);
$this->set_method($method);
// Assign routed segments, index starting from 1
$this->uri->rsegments = array(
1 => $class,
2 => $method
);
log_message('debug', 'No URI present. Default controller set.');
}
}
Upvotes: 0
Reputation:
Try changing controller with:
public function index()
{
$this->load->view('home');
}
Upvotes: 1
Reputation:
Change This
$route['default_controller'] = 'frontend/pages';
Change To
$route['default_controller'] = 'frontend/pages';
$route['default_controller'] = "pages";
Upvotes: 2