Can't access a Codeigniter controller in sub folder
I have set up a folder with a controller in it: controllers/admin/home.php
, but I get a 404 from the browser when开发者_StackOverflow社区 I try to access it.
This is my routes file:
$route['employers'] = "employers/home";
//$route['employers/dash'] = "employers/dash";
$route['default_controller'] = "home";
$route['404_override'] = '';
This is the controller file:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class home extends CI_Controller {
function __construct(){
parent::__construct();
/*
enable profiler
*/
//$this->output->enable_profiler(TRUE);
$this->load->helper('url');
$this->load->library('ion_auth');
$this->load->library('session');
$this->load->library('form_validation');
$this->load->helper('layout');
}
}
.htaccess
seems fine standard. Any ideas on what i'm doing wrong?
Note some things: 1) Routes are executed in the order they're written, and your custom routes MUST follow the default ones. So, it should be:
$route['default_controller'] = "home";
$route['404_override'] = '';
$route['employers'] = "employers/home";
This if your controller "home" is inside the folder "employers".
2) Controllers don't need all that stuff you wrote, indeed you don't even need to call the parent constructor unless you're planning to load libraries and resource for the whole controller's methods (which can be achieve also by autoloading them in the autoload.php file), so it could simply be:
file: application/controllers/employers/home.php
class Home extends CI_Controller {
function index()
{
// this is the method you're calling with your URL!
}
}
3) As per above, and as already pointed out by @Wesley, with your url you're trying to access the INDEX method of your controller HOME in your subfolder EMPLOYERS. But you didn't defined an index() method (which is the one called by default if no other is supplied). It seems, instead, that CI is trying to look for an employers controller and a home method; if it doesnt find it, but you have a employers folder, it tries to access the index method in the home controller in the employers folder. And, since it didn't find it either, you're getting the 404 page.
Hope I'm clear, otherwise just ask.
You failed to say how you were trying to access it via url. It should be:
{YOUR_BASE_URL}admin/home
... followed by optional URL segments (/method/param1/param2/etc
).
Without the additional segments, this would by default load the index
method. However, since you don't have any methods defined, there's nothing to load.
If this still fails after you define a method, move the controller file out of the sub-directory for starters, and make sure it works.
精彩评论