CodeIgniter正确设置路由

时间:2016-03-04 17:10:32

标签: php codeigniter routing

我尝试设置我的codeigniter路由以为管理面板制作身份验证表单,但路由无法正常工作。我是CodeIgniter的初学者,我想我错过了什么。

在我的服务器中,我将CodeIgniter fiels放在我的根目录中的文件夹中,因此可以像这样访问:

/localhost/ci

在我的config.php中,我设置了我的基本URL并删除索引页面以删除index.php:

$config['base_url'] = 'http://localhost/ci/';
$config['index_page'] = '';

我也编辑了.htaccess以删除index.php,就像CodeIgniter文档所说的那样,所以它看起来像这样:

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

现在,我的路线配置文件看起来像这样。我将默认页面和路径设置为我的Auth控制器:

$route['default_controller'] = 'auth';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['auth/login'] = "auth/login";

这是我的控制器:

类Auth扩展CI_Controller {

function __construct() {
    parent::__construct();
    // load libraries
}

function index() {
    if (! $this->ion_auth->logged_in()) {
        // redirect them to the dashboard page
        redirect(base_url('auth/login'), 'refresh');
    } else {
        // show the dashboard page
        redirect(base_url('dashboard'), 'refresh');
    }
}

function login() {
    $this->load->view('login');
}

当我进入网络浏览器http://localhost/ci/时,它会将我重定向到http://localhost/ci/auth/login,但随后会出现404 Not Found错误。我错过了什么?我此时有点困惑,谢谢。

1 个答案:

答案 0 :(得分:1)

尝试更改.htaccess:

RewriteEngine on RewriteCond $1 !^(index\.php|images|robots\.txt) RewriteRule ^(.*)$ /index.php/$1 [L]

RewriteEngine on RewriteCond $1 !^(index\.php|images|robots\.txt) RewriteRule ^(.*)$ /ci/index.php/$1 [L]