【问题标题】:Error 404 when trying to access a controller in CodeIgniter尝试访问 CodeIgniter 中的控制器时出现错误 404
【发布时间】:2015-11-04 11:21:31
【问题描述】:

我正在尝试访问 CodeIgniter 控制器,但遇到 404 错误。 在我的电脑里,我可以访问.../index.php/apadrinhamentoController/padrinhosPossiveis

但是当上传到服务器时,我只能访问索引文件,在 site.com/apadrinhamento/index.php 而不是 site.com/apadrinhamento/index.php/apadrinhamentoController(得到 404 错误)。

apadrinhamentoController.php文件

<?php
class apadrinhamentoController extends CI_Controller{
public function __construct() {
    parent::__construct();
    $this->load->helper('url');
}
public function index() {
    $this->load->view('apadrinhamento');
}
}

index.php 文件是 CodeIgniter 的默认文件

我没有在/apadrinhamento 上使用.htaccess

【问题讨论】:

  • 你在路由中做了哪些必要的设置,配置文件你会为此发布代码
  • 绝对可以是您没有向我们展示的任何内容。 index.php 文件通常位于 www 根目录中,而不是子目录中。您的.htaccess 文件中有什么内容?你的config.php 文件呢? index.php 文件中的各种设置?到目前为止,您自己尝试过什么来解决这个问题?
  • index.php 文件是 CodeIgniter 的默认文件...我没有在这个文件夹上使用 .htaccess 文件
  • 那是不可能的。如果不为您的特定安装配置几行代码,默认的index.php 将无法工作。文档中对此进行了说明。
  • 我在根目录中安装了 wordpress,在子文件夹中安装了 codeigniter ...

标签: php codeigniter http-status-code-404


【解决方案1】:

首先你应该明白,Codeigniter != CakePHP。意味着你不需要定义controller_name+Controller这个词。

class apadrinhamento extends CI_Controller
{
    public function __construct() 
    {
        parent::__construct();
        $this->load->helper('url');
    }
    public function index() 
    {
        //$this->load->view('apadrinhamento');
        echo 'Im index of your function';
    }

    public function my_method() 
    {
        echo 'I jsut access my_Method';
    }

}

config/routes.php

$route['default_controller'] = "";//Default Controller Name
$route['404_override'] = '';

访问您的控制器(上)

site.com/apadrinhamento/ //this will access index() function
//or
site.com/index.php/apadrinhamento/

如果你想访问里面的方法(URL应该是)

site.com/apadrinhamento/my_method
//or
site.com/index.php/apadrinhamento/my_method

注意如果你没有放置.htaccess,那么url应该是site.com/index.php/apadrinhamento/

【讨论】:

    【解决方案2】:

    问题是在 CodeIgniter v3.0 中我们需要编写以大写字母开头的控制器文件名。

    【讨论】:

    • 但不知何故,它仍然可以在您的本地计算机上运行。另一个问题是,在您的 OP 中,您没有提及您使用的是哪个版本的 CodeIgniter。请查看:How to Ask
    猜你喜欢
    • 2011-11-29
    • 1970-01-01
    • 2017-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多