Codeigniter making sub-directory controllers work

2019-01-25 13:55发布

I'm having trouble making the page load the controller I want it too.

For example I want my site to load as localhost/sitename/catergory1/catergory2 where catergory 1 is its own controller and 2 is a method. So i've tried adding this to my routes.php:

$route['catergory1'] = 'catergory1/cat1';

with my controller file set up as:
-Controller
   -Home.php
   -catergory1 <- subfolder
       -cat1.php
which I thought would cause codeigniter to load the controller 'cat1' inside the catergory 1 folder but instead when I go to localhost/sitename/catergory1 it just loads my default controller 'home'. I've tried putting it in both the uri routes and reserved routes part of routes.php but it still won't work. It's probably something really easy but im new to this.

Heres the controllers themselves just incase its something to do with them:
home controller:

class Home extends CI_Controller {  
    function index() {  
        $this->load->view('home');  
    }
}

Cat1 controller:

class Cat1 extends CI_Controller{  
    function index() {  
        $this->load->view('cat1');  
    }  
}

Am i just being stupid and missing something easy?
Thanks.

1条回答
再贱就再见
2楼-- · 2019-01-25 14:37

You have a typo in category1 btw.

If I follow correctly you have this structure:

\ controllers
 - home.php
 \ category1 \
  - cat1.php

If so, you confused yourself with your route:

$route['category1 '] = 'category1/cat1/index';

Need to define the method index, this is based on your 'cat1.php' file being:

class Cat1 extends CI_Controller{
   function index() {
      $this->load->view('cat1');
   }
}

I wrote about this, as people usually get confused with routes in CI:
http://blog.biernacki.ca/2011/12/codeigniter-uri-routing-issue-with-controller-folders/

查看更多
登录 后发表回答