【问题标题】:Codeigniter controller action routing issueCodeigniter 控制器动作路由问题
【发布时间】:2017-08-14 06:28:57
【问题描述】:

我是 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 中请求 url 时,它显示 404 Page Not Found

【问题讨论】:

    标签: codeigniter codeigniter-3


    【解决方案1】:
    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/';
    

    【讨论】:

    • 检查 rewrite_mode 在 apache(xampp 或 wamp)中是否开启
    【解决方案2】:

    使用它肯定会为您工作。 不要做任何其他一切都是完美的。 只替换 .htaccess 代码

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

    注意:确保 rewrite_mode 在 apache 中开启

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-09
      • 2015-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-05
      • 1970-01-01
      相关资源
      最近更新 更多