OOP PHP Codeigniter 控制器层次结构

发布于 2024-11-05 23:55:35 字数 700 浏览 1 评论 0原文

我的 OOP 概念有点问题。我会尽力解释。

我有这个类

class Application_controller extends CI_Controller{
     public function addItem(){
        "some code to add the item to the database (working)";
     }
}

,我还有另一个类,两个控制器:

require_once 'application_controller.php';
class Contact extends Application_controller{
     public function __construct(){
         parent::__construct("variables needed");
     }
}

在联系人的视图添加中,我添加了以下操作contact/addItem

好的,现在这就是我对 OOP 的总体了解。

方法 addItem 是否不应该是 Contact 类的一部分,因为它扩展了 Application_controller?

我之所以这么问,是因为当我提交表单时,我没有得到任何操作,而当我在 Contact 类中添加方法 addItem 并覆盖父类时,它就会起作用。

I'm having a litle problems with my concepts of OOP. I'll try to explain the best I can.

I have this class

class Application_controller extends CI_Controller{
     public function addItem(){
        "some code to add the item to the database (working)";
     }
}

And I have another class, both controllers:

require_once 'application_controller.php';
class Contact extends Application_controller{
     public function __construct(){
         parent::__construct("variables needed");
     }
}

And in the View add of the contact I added the following action contact/addItem.

Ok, now here's what I know about OOP in general.

Isn't the method addItem supposed to be part of the Contact class because its extends Application_controller?

I'm asking because when I submit the form I get no action, and when I add the method addItem in the class Contact overriding the parent one it works.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

随遇而安 2024-11-12 23:55:35

您没有采取任何行动的原因是 codeigniter 在您的 Contact 类中找不到方法 addItem (更新:这可能是由于 CodeIgniter 路由的工作方式所致)。解决方案是使 addItem 成为模型中的通用方法,将数据存储在表中,将其移动到模型,然后将模型加载到控制器中。

创建 application/models/writeModel.php

class writeModel extends CI_Model{
  function addItem(){
    // code here
  }
}

在控制器中

    class Contact extends Controller{
       function __controller(){
          parent::Controller();
          $this->load->model('writeModel');
       }

       function somefunction(){
          $this->writeModel->addItem(); // call the method here
       }
    }

:参考:CodeIgniter 模型

The reason you get no action is that codeigniter doesn't find a method addItem in your Contact class (update: this is probably due to the way CodeIgniter routing works). The solution would be to make addItem a generic method in a Model that stores data in a table, move it to a Model, and load the model in your controller.

Create application/models/writeModel.php

class writeModel extends CI_Model{
  function addItem(){
    // code here
  }
}

In your controller:

    class Contact extends Controller{
       function __controller(){
          parent::Controller();
          $this->load->model('writeModel');
       }

       function somefunction(){
          $this->writeModel->addItem(); // call the method here
       }
    }

Reference: CodeIgniter Models

物价感观 2024-11-12 23:55:35

这里的问题(除了OP中的几个语法错误之外)很可能是“Contact”无法扩展“Application_controller”,因为它不知道它存在。如果我们设置这样的测试:

/controllers/Test.php

class Test extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
    }

    function index()
    {
        echo 'test';
    }
}

/controllers/TestTwo.php

require_once("Test.php");

class TestTwo extends Test    
{
    function __construct()
    {
        parent::__construct();
    }
    function index()
    {
        parent::index();
        echo ' and test two';
    }
}

我们将通过导航到“测试和测试二”获得所需的输出appurl/TestTwo/.这是因为 TestTwo 知道 Test。删除 require(); TestTwo.php 中的行将破坏关系。

从 TestTwo 中删除 index() 函数将导致导航到 appurl/TestTwo/ 时仅输出“test”。

The problem here (other then the several syntax errors in the OP) is likely to be that "Contact" can not extend "Application_controller" because it does not know it exists. If we setup a test like this:

/controllers/Test.php

class Test extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
    }

    function index()
    {
        echo 'test';
    }
}

/controllers/TestTwo.php

require_once("Test.php");

class TestTwo extends Test    
{
    function __construct()
    {
        parent::__construct();
    }
    function index()
    {
        parent::index();
        echo ' and test two';
    }
}

We will get the desired output of "test and test two" by navigating to appurl/TestTwo/. This is because TestTwo knows of Test. Removing the require(); line from TestTwo.php will break the relation.

Removing the index() function from TestTwo will then result in only "test" being output by navigating to appurl/TestTwo/.

萌酱 2024-11-12 23:55:35

我在 Codeigniter 论坛上找到了一些类似问题的答案。它说这个

您的 ShopDownloads 将从 Shop 控制器继承(方法、属性等)。正如视频教程中所说,您必须从控制器类继承您的类,以便它可以继承codeigniter为您提供的所有属性和方法。

Sohaib,

该帖子的链接是 http://codeigniter.com/forums/viewthread/102718/ #518120

我不知道怎么做,但今天可以用了。可能是服务器。只需要重新启动即可。

解决了,只需今天启动服务器并开始开发哈哈。谢谢你们的宝贵时间。

问候,

埃尔卡斯

I found an answer to some similar question on the Codeigniter forums. It says this

your ShopDownloads will inherit (methods,properties etc etc) from the Shop controller. and as said in the video tutorial, u must inherit your class from the controller class so that it can inherit all the properties and methods codeigniter provides for u.

Sohaib,

The link for the post is http://codeigniter.com/forums/viewthread/102718/#518120

I don't know how but this is working today. It was probably the server. Just needed a restart.

Its Solved, just by start the server today and start developing LOL. Thanks for youre time guys.

Regards,

Elkas

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