OOP PHP Codeigniter 控制器层次结构
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您没有采取任何行动的原因是 codeigniter 在您的 Contact 类中找不到方法 addItem (更新:这可能是由于 CodeIgniter 路由的工作方式所致)。解决方案是使 addItem 成为模型中的通用方法,将数据存储在表中,将其移动到模型,然后将模型加载到控制器中。
创建 application/models/writeModel.php
在控制器中
:参考: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
In your controller:
Reference: CodeIgniter Models
这里的问题(除了OP中的几个语法错误之外)很可能是“Contact”无法扩展“Application_controller”,因为它不知道它存在。如果我们设置这样的测试:
/controllers/Test.php
/controllers/TestTwo.php
我们将通过导航到“测试和测试二”获得所需的输出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
/controllers/TestTwo.php
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/.
我在 Codeigniter 论坛上找到了一些类似问题的答案。它说这个
Sohaib,
该帖子的链接是 http://codeigniter.com/forums/viewthread/102718/ #518120
我不知道怎么做,但今天可以用了。可能是服务器。只需要重新启动即可。
解决了,只需今天启动服务器并开始开发哈哈。谢谢你们的宝贵时间。
问候,
埃尔卡斯
I found an answer to some similar question on the Codeigniter forums. It says this
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