1

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);
    }
}
Pradeep
  • 9,667
  • 13
  • 27
  • 34
M_Idrees
  • 2,080
  • 2
  • 23
  • 53

5 Answers5

1

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';
Pradeep
  • 9,667
  • 13
  • 27
  • 34
1

Change This

$route['default_controller'] = 'frontend/pages';

Change To

$route['default_controller'] = 'frontend/pages';
$route['default_controller'] = "pages";
1

Try changing controller with:

 public function index()
    {
        $this->load->view('home');
    }
0

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.');
    }
}
  • Tried to make it bit more clear pretty easy for to understand. –  May 19 '18 at 10:54
  • Thanks for adding explanation. So for controllers in subfolders, it needs to create custom router inheriting default one and then set variables like $directory, $class, $method manually to that it can load these things from subfolder. – M_Idrees May 19 '18 at 11:02
  • @M_Idrees All other controllers are fine in sub folders but just the one that you want to have as a default controller can not be. Unless you create that MY_Router.php file example I have. –  May 19 '18 at 11:04
  • Yes got it. Thanks @Mr. ED, it helps to understand. – M_Idrees May 19 '18 at 11:06
-1

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';
nandal
  • 2,544
  • 1
  • 18
  • 23
  • still getting same response. – M_Idrees May 19 '18 at 10:04
  • I know it's just for 404 error page, but the user has clearly mentioned in the query, that **I want to render home page if there is no path after website root**. Therefore, overriding of 404 error page will help to render the default page in case there's no resource available for the given url – nandal May 19 '18 at 18:24