Akshay Malhotra
Akshay Malhotra

Reputation: 49

Subfolders inside controllers give 404

I am using Codeigniter 3.1.10. I am trying to create an admin and user sites separately with the following folder structure

in route.php I have following:

$route['default_controller'] = 'home';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

which I tried changing to

$route['default_controller'] = 'site/home';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

It doesn't work.

I want when I go to http://example.com/site it should use site/home.php controller and when I go to http://example.com/admin it should use admin/home.php controller

As per the official document, when you set a default controller, it should search for the default controller defined in a subfolder, but it does not work. https://www.codeigniter.com/userguide3/installation/upgrade_300.html#directories-and-default-controller-404-override

Many people have fixed it by making changes in the core file of Codeigniter. But I want to do it without making changes in the core files.

Upvotes: 1

Views: 148

Answers (4)

Justin J
Justin J

Reputation: 832

For setting the default controller for you folder sub-folder to home add the following to your /application/config/routes.php file:

$route['folder'] = "folder/home";

Upvotes: 0

Sherif Salah
Sherif Salah

Reputation: 2153

You cannot use sub_dirs with default controller .. but if you have to do so create a normal route as following:

$route['default_controller'] = 'home';

And inside home controller you may use redirect to site/home.

Upvotes: 1

Elena Vasilenko
Elena Vasilenko

Reputation: 236

The $route['default_controller'] is used for the root address without segments http://example.com/. But you have addresses with segments http://example.com/site and http://example.com/admin.

I want when I go to http://example.com/site it should use site/home.php controller and when I go to http://example.com/admin it should use admin/home.php controller

You need to do this in your routes.php:

$route['site'] = 'site/home';
$route['admin'] = 'admin/home';

Upvotes: 0

Deepak Kumar
Deepak Kumar

Reputation: 31

use in routes.php

$route['default_controller'] = 'home';

create controller inside admin folder home.php

class Home extends CI_Controller {
    public function index()
    {
        echo "demo text";       
    }
}

Then hit "http://example.com/admin" or "http://example.com/index.php/admin". if second one url work it means you add .htaccess file application root

Upvotes: 1

Related Questions