为什么在 CodeIgniter 中调用两个控制器方法?

发布于 2024-11-28 15:21:02 字数 714 浏览 2 评论 0原文

我不应该使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁月如刀 2024-12-05 15:21:02

为了进一步澄清问题所在,在 PHP4 构造函数中,有一个与类同名的函数...

example

class MyClass
{
    public function MyClass()
    {
        // as a constructor, this function is called every 
        // time a new "MyClass" object is created
    }
}

现在对于 PHP5 版本(从 2.0.x 开始,现在的 codeigniter 持有作为系统要求)

class MyClass
{
    public function __construct()
    {
        // as a constructor, this function is called every 
        // time a new "MyClass" object is created
    }
}

所以要回答解决该问题的问题...

我不应该使用 Index 作为 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

class MyClass
{
    public function MyClass()
    {
        // as a constructor, this function is called every 
        // time a new "MyClass" object is created
    }
}

Now for the PHP5 version (Which codeigniter now, as of 2.0.x, holds as a system requirement)

class MyClass
{
    public function __construct()
    {
        // as a constructor, this function is called every 
        // time a new "MyClass" object is created
    }
}

So To answer the question that addresses the problem...

Should I not be using Index as the name for a controller class in CodeIgniter?

I believe it would be best to not choose Index as a controller name as the index() function has a reserved use in codeigniter. This could cause issues depending on your PHP configuration.

ヅ她的身影、若隐若现 2024-12-05 15:21:02

谁能告诉我为什么会发生这种情况?

当你的控制器被实例化时,index作为构造函数被调用。

比较构造函数和析构函数文档

为了向后兼容,如果 PHP 5 找不到给定类的 __construct() 函数,它将通过类的名称搜索旧式构造函数。 [我突出显示]

在您的情况下,您的控制器没有任何 __construct() 函数,但有一个与类同名的函数:index。它在 Codeigniter 解析并加载并实例化您的 Index 控制器时被调用。

您可以通过将构造函数添加到控制器来解决此问题:

class Index extends CI_Controller
{
    public function __construct() {}
    public function index()
    {
        echo "index";
    }
    public function blah()
    {
        echo "blah";
    }
}

进行此更改后,它不会再次发生。

我应该将此作为错误报告给 CodeIgniter 吗?

不,实际上没有必要将其报告为错误,这就是语言的工作方式,并且由于 Codeigniter 支持 PHP 4,因此它必须保持向后兼容并需要提供 PHP 4 构造函数。 (注意:Codeigniter 项目文档,它们需要 PHP 版本 5.1.6 或更高版本的服务器支持,但实际代码内置了 PHP 4 兼容性,我指的是这里的代码库,而不是文档。)

can anyone tell me why this is happening?

When your controller get's instantiated, index as the constructor is getting called.

Compare Constructors and DestructorsDocs:

For backwards compatibility, if PHP 5 cannot find a __construct() function for a given class, it will search for the old-style constructor function, by the name of the class . [highlighting by me]

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 your Index Controller.

You can solve this by just adding the constructor to your Controller:

class Index extends CI_Controller
{
    public function __construct() {}
    public function index()
    {
        echo "index";
    }
    public function blah()
    {
        echo "blah";
    }
}

After this change, it does not happen again.

Should I report this as a bug to CodeIgniter?

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.)

凉薄对峙 2024-12-05 15:21:02

这是使用 Codeigniter3 的另一个解决方案

require_once 'Base.php';
        class Index extends Base
    {
        public function __construct()
        {
        parent::index();
        $classname=$this->router->fetch_class();
    $actioname=$this->router->fetch_method();

    if($actioname=='index' || $actioname == '')
    {
        $this->viewall();
    }
}
}

,viewall() 具有以下内容

$this->siteinfo['site_title'].=' | Welcome';
$this->load->view('templates/header', $this->siteinfo);
$this->load->view('templates/menu', $this->siteinfo);
$this->load->view('index/viewall', $data);
$this->load->view('templates/footer', $this->siteinfo);

基本控制器为整个应用程序加载所有库和帮助程序,这就是默认类中需要它的原因

基本上从我对 CodeIgniter 的简短理解来看,有一个默认的将动作作为索引是错误的。我通过打印 $this->router->fetch_method(); 的结果发现了这一点在我的索引类的construct() 中。 CodeIgniter 的默认操作是索引,您只能在 application/config/routes.php 中设置默认控制器,而不能设置默认操作。

所以我的建议是,永远不要使用index()作为默认操作,特别是如果您使用index作为默认控制器

Here is another solution using Codeigniter3

require_once 'Base.php';
        class Index extends Base
    {
        public function __construct()
        {
        parent::index();
        $classname=$this->router->fetch_class();
    $actioname=$this->router->fetch_method();

    if($actioname=='index' || $actioname == '')
    {
        $this->viewall();
    }
}
}

And the viewall() had the following

$this->siteinfo['site_title'].=' | Welcome';
$this->load->view('templates/header', $this->siteinfo);
$this->load->view('templates/menu', $this->siteinfo);
$this->load->view('index/viewall', $data);
$this->load->view('templates/footer', $this->siteinfo);

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文