CodeIgniter加载唯一的默认控制器


CodeIgniter loads the only default controller

我真的被这个问题卡住了

在CodeIgniter中,我有一个名为user.php 的控制器

它有两个函数getUser()和save()

当我尝试呼叫http://localhost/CodeIginter/index.php/user/save

为什么总是调用getUser函数?

class user extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
    }
    function getUser()
    {
        $this->load->model('usermodel');
        $data['query'] = $this->usermodel->get_last_ten_entries();
        $this->load->view('users',$data);
    }
    function save()
    {
        $this->load->model('usermodel');
        $this->usermodel->insert_entry();
        $this->load->view('users');
    }
}

我的.htaccess文件包含

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

我也无法加载另一个控制器helloworld.php

步骤1:更改此.htaccess

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteBase /your_folder_name/
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

步骤2:application/config/config.php

$config['base_url'] = 'full_path';
$config['index_page'] = '';

步骤3:application/config/routes.php

$route['default_controller'] = "user";

步骤4:用户类添加此功能

public function index(){
}

可能有两个问题:

1.缺少路线

转到应用程序/路由.php

$route['user'] = "user";

2.如果您想在url中从外部调用函数,则需要将其公开。

所以,

class user extends CI_Controller
{
function __construct()
{
    parent::__construct();
}
public function getUser()
{
    $this->load->model('usermodel');
    $data['query'] = $this->usermodel->get_last_ten_entries();
    $this->load->view('users',$data);
}
public function save()
{
    $this->load->model('usermodel');
    $this->usermodel->insert_entry();
    $this->load->view('users');
}
}

希望您的问题现在得到解决:)

控制器名称必须大写。因此,User而不是user

class User extends CI_Controller
class Helloworld extends CI_Controller
...

不过,文件名仍然是小写的。

除此之外,一切似乎都很好。

转到application/config/routes.php

您似乎错过了一条路线。在routes.php文件中添加以下行

$route['user'] = "user";

现在它应该像预期的那样工作