Search code examples
phpcodeignitercontrollerssubdirectory

using subfolders in codeigniter causes problems


I am getting page not found error in my CI web app I have 3 seperate sub-folders with controllers inside of them, admin, site, members. the structure looks like so.

- Controllers
--- Site
----- site.php <-- handles all general site pages
--- Members
----- dashboard.php <-- default controller to be called when no parameter is passed
----- products.php <-- handles all products request
--- Admin
---- dashboard.php <-- default controller to be called when no parameter is passed
---- members.php <-- handles all members request

I tried routing it in the routes.php file like this

// Admin - folder/controler/Method

$route['admin/(:any)'] = 'admin/admin/$1';
$route['admin'] = 'admin/dashboard/index'; 

$route['clients/(:any)'] = 'clients/$1';
$route['clients'] = 'clients/dashboard/index';

$route['(:any)'] = 'site/$1';
$route['default_controller'] = 'site/index';

$route['404_override'] = '';

What do I do to fix this?


Solution

  • Please try

    // Admin - folder/controler/Method
    
    $route['default_controller'] = 'site';
    $route['404_override'] = '';
    
    $route['admin/(:any)'] = 'admin/admin/$1';
    $route['admin'] = 'admin/dashboard'; 
    
    
    $route['clients/(:any)'] = 'clients/$1';
    $route['clients'] = 'clients/dashboard';
    
    
    
    $route['(:any)'] = 'site/$1';
    
    • get rid of method (index)
    • different order
    • let reserved routes on top

    Order of routes is important, when CI finds the first valid route it won't do the other routes in the list.