Search code examples
phpcodeigniterdirectory-structure

How to use folders in Codeigniter?


I'm just starting with Codeigniter. I created a simple controller called home.php and a view called home_view.php. This is working fine. Now I would eventually like this site I'm building to have a admin section in addition to the public www version. So I reorganized my file structure like so:

controllers:

www
 - home.php
admin

Views:

www
 - home_view.php
admin

Eventually I will put the admin related controllers and views in their respective directories. But after having moved my files like so, they no longer work. I think I need to changes something in the routes or config file. What do I have to do?


Solution

  • For the views you just need to add the folder name to the begin of the view like:

    $this->load->view('www/home_view.php');
    

    You will probably need to redo the routing for the controllers, so that extra folders are accounted for. This can be done with something like the following:

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

    This will use the controller($1) and function($2) inside the admin folder if the url is www.example.com/index.php/admin/[controller]/[function]

    for the controllers in the admin folder;

    and update the default controller like this:

    $route['default_controller'] = "www/home";