Search code examples
controllerroutessubdirectorycodeigniter-4

codeigniter4 controller routing in subfolder


I have this setup in Routes:

$routes->add("admin", "Admin/Login::index");
$routes->add("login", "Admin/Login::index");
$routes->add("admin/home", "Admin/Admin::index");

my controller directory is like this:

folder structure

Problem: when I access:

http://mydevsite.vhost/admin/home
http://mydevsite.vhost/login

both are working as I want it, but this link:

http://mydevsite.vhost/admin

returns 403 Forbidden.

how should I write my route or controller to make /admin goes to the same place as /admin/home or something else?


Solution

  • The problem occurred because I have a folder in my public folder that shares the same name with my folder in controller which is "admin" so when i access /admin it search for index.php inside public/admin.

    and more info about routes

    $routes->add("admin", "Admin\Login::index");
    $routes->add("login", "Admin\Login::index");
    $routes->add("admin/home", "Admin\Admin::index");
    

    that is the correct syntax, using / will cause an 404 error when the link have a variable segment.