Codeigniter调用函数在没有index.php的控制器中
我知道有这么多帖子认为这个问题。我尝试了一切,但没有成功。Codeigniter调用函数在没有index.php的控制器中
我在我的windows 10机器上使用xampp v3.2.2。在我的htdocs中,我有一个名为mysite的项目。在那里我有codeigniter 3.0.3。
的config.php
$config['base_url'] = 'http://localhost/mysite/';
$config['index_page'] = '';
routes.php文件
$route['default_controller'] = 'CI_home';
控制器CI_home.php:
class CI_home extends CI_Controller{
function __construct() {
parent::__construct();
}
public function index($lang = ''){
$this->load->helper('url');
$this->load->helper('language');
$this->lang->load($lang, $lang);
$this->load->view('view_home');
}
}
当我调用http://localhost/mysite
,页面被正确显示。
问题是,当我尝试http://localhost/mysite/en
或http://localhost/mysite/index/en
我从xampp收到404。
但是,如果我尝试http://localhost/mysite/index.php/CI_home/index/en
它工作正常。
我做错了什么?我如何删除“CI_home/index”?
http://localhost/mysite/.htaccess:
的.htaccess
RewriteEngine On
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
如果启用mod_rewrite的我已经选中。
请帮帮我!
由于提前, yab86
尝试以下
Options +FollowSymLinks
Options -Indexes
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
这htaccess的确保htaccess的在你的主目录。
然后设置自己的基本网址
$config['base_url'] = 'http://localhost/yourproject/';
然后取出的index.php
$config['index_page'] = '';
注:笨3个版本是区分大小写的。 确保只有类别和文件名称的第一个字母大写。
<?php
class Ci_home extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function index($lang = '') {
$this->load->helper('url');
$this->load->helper('language');
$this->lang->load($lang, $lang);
$this->load->view('view_home');
}
}
然后确保文件名是Ci_home.php
那么你的默认路由应该是
当使用默认的控制器确保是相同的名称作为您选择的控制器。
$route['default_controller'] = 'ci_home';
$route['(:any)'] = 'ci_home/index/$1';
这是字母大写的问题,谢谢wolfgang1983! – yab86
添加到您的route.php'$路线[ '(:任何)'] = 'CI_home/$ 1';' – MAZux
谢谢MAZux。我试过了,但没有改变。 – yab86
尝试添加索引方法,试试:'$ route ['(:any)'] ='CI_home/index/$ 1';' – MAZux