【问题标题】:Codeigniter call Function in Controller without index.phpCodeigniter 在没有 index.php 的控制器中调用函数
【发布时间】:2016-11-24 08:10:50
【问题描述】:

我知道有很多关于这个问题的帖子。我尝试了一切,但没有成功。

我在我的 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/enhttp://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

【问题讨论】:

  • 添加到你的 route.php $route['(:any)'] = 'CI_home/$1';
  • 谢谢 MAZux。我试过了,但没有任何改变。
  • 也尝试添加索引方法,尝试:$route['(:any)'] = 'CI_home/index/$1';
  • 你用的是wamp、xampp、lamp等哪一个

标签: php apache .htaccess codeigniter mod-rewrite


【解决方案1】:

试试下面这个htaccess

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 在您的主目录中。

然后设置你的基本网址

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

然后删除 index.php

$config['index_page'] = '';

注意: Codeigniter 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

那么你的默认路由应该是

使用默认控制器时,请确保与您选择的控制器名称相同。

URI Routing

$route['default_controller'] = 'ci_home';

$route['(:any)'] = 'ci_home/index/$1';

【讨论】:

  • 这是字母大写的问题。谢谢 wolfgang1983!
猜你喜欢
  • 2015-03-01
  • 2019-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-22
  • 1970-01-01
  • 2015-11-09
  • 1970-01-01
相关资源
最近更新 更多