1

我是codeigniter 3.x的新手,在我写的时候是codeigniter 3.x

class Auth extends CI_Controller {

    public function __construct() {
        parent::__construct();
        echo "ya";
    }
}

它向我显示错误

404 Page Not Found

The page you requested was not found.

当我写

class Auth extends CI_Controller {

    public function __construct() {
        parent::__construct();
        echo "ya";
    }
    public function index() {
        echo "aya";exit;
    }
}

它工作正常并将输出显示为 {yaaya}。谁能让我知道你是这个吗?

4

2 回答 2

1

这背后的原因是当你运行 url

http://ip/cifolder/index.php/controller

默认情况下,它会查看index()此控制器

如果您使用 url 之类的

http://ip/cifolder/index.php/controller/function

它将查看您的控制器的该功能

如果您没有在控制器中编写任何函数,只需 __construct

function __construct() {
        parent::__construct();
    }

这意味着没有 index() 函数它会告诉你400 error

于 2015-05-15T11:51:21.530 回答
0

index()发生这种情况是因为当您没有提供要采取的行动时,CI 正在寻找它。对于第一种情况,它不存在,所以它抛出了错误。但在第二种情况下,它就在那里,所以它起作用了。你不能__construct()明确地打电话。

它寻找的默认 url 模式controller/action。如果action未提供,它将index()在该控制器中查找。

因此,当它获得 时index(),它会实例化控制器类并__construct()调用 get 。

于 2015-05-15T11:50:37.240 回答