Wilky
Wilky

Reputation: 1

404 error on controller within several directories,

I am using Codeigniter 2.0.1. When I call for example..

http://localhost/index.php/admin/login ./controllers/admin/login.php

It works fine but when I call the url below with a folder within the admin directory it throws the 404 error.

http://localhost/index.php/admin/new_dir/dashboard ./controllers/admin/new_dir/dashboard.php

My controllers are placed and named correctly. Does anyone know why this is happening or can controllers only be one directory deep from the controller directory?

Thanks

Upvotes: 0

Views: 440

Answers (2)

qwertzman
qwertzman

Reputation: 792

Yes it is possible to have subfolders in application/controller.

  1. routes.php: $route['default_controller'] = "default_page";

  2. Add a *default_page.php* to each sub-folder and the controller folder itself, than redirect to the actual controller you need to use as default for that folder, e.g.:

    class Default_Page extends Public_Controller {

        public function __construct()
        {
            parent::__construct();
            redirect(Settings_model::$db_config['home_page']);
        }
    }
    
  3. Side-effect: your custom 404 handlers - if you have any - will get messed up: sub-folders are not caught and will throw the default error_404. I'm still looking for a decent solution for this.

Disadvantage: You have similar files for each folder and sub-folder containing almost the same code. Suppose you have a large project with many folders this will become hard to maintain (at which point it would be better to use HMVC). I hope someone has a better solution but at the least this works.

Upvotes: 0

eyaka1
eyaka1

Reputation: 385

Unfortunately you can only have one level of subdirectory in Codeigniter. Only the first URI segment is checked as a possible directory.

Do you wish to have an extra subdirectory for neatness sake or is this for the benefit of your URIs? If it is the latter you can emulate the behavior of extra subdirectories using routes.

Upvotes: 2

Related Questions