为什么在 CodeIgniter 中调用两个控制器方法?
我不应该使用 Index
作为 CodeIgniter 中控制器类的名称吗?我有一个索引控制器,并且我看到它的方法被多次调用。更具体地说,我总是看到它的 index
方法首先被调用,无论我是否正在访问应该路由到那里的路径。
在 application/controllers/index.php
class Index extends CI_Controller
{
public function index()
{
echo "index";
}
public function blah()
{
echo "blah";
}
}
当我访问 index/blah
时,我看到 indexblah
打印出来。当我访问 index/index
时,我看到 indexindex
。如果我将控制器重命名为其他名称(例如 Foo),则不会有问题。这是显而易见的解决方法,但有人能告诉我为什么会发生这种情况吗?我应该将此作为错误报告给 CodeIgniter 吗?
(注意:我没有在 configs/routes.php
中设置路由;我的 index.php
在 CodeIgniter 树之外)
Should I not be using Index
as the name for a controller class in CodeIgniter? I have an Index controller, and I'm seeing its methods being called multiple times. More specifically, I always see its index
method called first, whether or not I'm visiting a path that should be routed there.
In application/controllers/index.php
class Index extends CI_Controller
{
public function index()
{
echo "index";
}
public function blah()
{
echo "blah";
}
}
When I visit index/blah
, I see indexblah
printed. When I visit index/index
, I see indexindex
. If I rename the controller to something else (e.g. Foo), it doesn't have a problem. That's the obvious workaround, but can anyone tell me why this is happening? Should I report this as a bug to CodeIgniter?
(Notes: I have no routes set up in configs/routes.php
; my index.php
is outside the CodeIgniter tree)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了进一步澄清问题所在,在 PHP4 构造函数中,有一个与类同名的函数...
example
现在对于 PHP5 版本(从 2.0.x 开始,现在的 codeigniter 持有作为系统要求)
所以要回答解决该问题的问题...
我认为最好不要选择
Index
作为控制器名称,因为index()
函数在 codeigniter 中保留使用。这可能会导致问题,具体取决于您的 PHP 配置。To further clarify what the issue is, in PHP4 Constructors were a function that had the same name as the Class...
example
Now for the PHP5 version (Which codeigniter now, as of 2.0.x, holds as a system requirement)
So To answer the question that addresses the problem...
I believe it would be best to not choose
Index
as a controller name as theindex()
function has a reserved use in codeigniter. This could cause issues depending on your PHP configuration.当你的控制器被实例化时,
index
作为构造函数被调用。比较构造函数和析构函数文档 :
在您的情况下,您的控制器没有任何
__construct()
函数,但有一个与类同名的函数:index
。它在 Codeigniter 解析并加载并实例化您的Index
控制器时被调用。您可以通过将构造函数添加到控制器来解决此问题:
进行此更改后,它不会再次发生。
不,实际上没有必要将其报告为错误,这就是语言的工作方式,并且由于 Codeigniter 支持 PHP 4,因此它必须保持向后兼容并需要提供 PHP 4 构造函数。 (注意:Codeigniter 项目文档,它们需要 PHP 版本 5.1.6 或更高版本的服务器支持,但实际代码内置了 PHP 4 兼容性,我指的是这里的代码库,而不是文档。)
When your controller get's instantiated,
index
as the constructor is getting called.Compare Constructors and DestructorsDocs:
In your case your Controller does not have any
__construct()
function but a function that has the same name as the class:index
. It is getting called in the moment Codeigniter resolves and loads and then instantiates yourIndex
Controller.You can solve this by just adding the constructor to your Controller:
After this change, it does not happen again.
No, there is not really a need to report this as a bug, it's how the language work and as Codeigniter supports PHP 4 it must remain backwards compatible and needs to offer PHP 4 constructors. (Note: The Codeigniter project documents, they need server support for PHP version 5.1.6 or newer, but the actual code has PHP 4 compatiblity build in, I'm referring to the codebase here, not the documentation.)
这是使用 Codeigniter3 的另一个解决方案
,viewall() 具有以下内容
基本控制器为整个应用程序加载所有库和帮助程序,这就是默认类中需要它的原因
基本上从我对 CodeIgniter 的简短理解来看,有一个默认的将动作作为索引是错误的。我通过打印 $this->router->fetch_method(); 的结果发现了这一点在我的索引类的construct() 中。 CodeIgniter 的默认操作是索引,您只能在 application/config/routes.php 中设置默认控制器,而不能设置默认操作。
所以我的建议是,永远不要使用index()作为默认操作,特别是如果您使用index作为默认控制器
Here is another solution using Codeigniter3
And the viewall() had the following
The Base controller does all the library and helper loading for the entire application which is why it is being required in the default class
Basically from my short understanding of CodeIgniter, having a default action as index is a wrong. I found this out by using the printing the result of $this->router->fetch_method(); in the construct() of my index class. The default action by CodeIgniter is index, you may only set the default controller within application/config/routes.php and not the default action.
So my advice, never use index() as the default action especially if you are using index as the default controller