Codeigniter控制器动作路由问题

时间:2017-08-14 06:28:58

标签: codeigniter codeigniter-3

我是Codeigniter的初学者。 在 .htaccess 文件

 RewriteEngine on
 RewriteCond $1 !^(index\.php|resources|robots\.txt)
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule ^(.*)$ index.php/$1 [L,QSA] 

在config.php文件中,我进行了更改

$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';
$config['url_suffix'] = '';
$config['base_url'] = '';

在route.php文件中,我进行了更改

$route['products/register']  = 'products/register';
$route['products'] = 'products';
$route['default_controller'] = 'products';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

产品控制器

<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed'); 
class Products extends CI_Controller {  
  public function __construct(){  
       parent::__construct(); 
        $this->load->helper(array('form', 'url'));
        $this->load->library(array('session', 'form_validation', 'email')); 
        $this->load->database();           
        $this->load->model("product_model");  
  }       
  public function index()  
  {  
         die("aaaaa"); exit;
  }  
  public function register()  
   {  
       die("DSds");
   }
 }

当我在浏览器http://localhost/codei/products/register中请求网址时,其显示404 Page Not Found

2 个答案:

答案 0 :(得分:0)

RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

在.htaccess中尝试上述代码,并在配置文件中设置基本URL

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

答案 1 :(得分:0)

使用它肯定会对你有用。 不做其他事情都是完美的。 只替换.htaccess代码

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
  

注意:确保在apache中启用了rewrite_mode