如何在Codeigniter中将业务逻辑与控制器分离。我正在使用带有Phills模板库的HMVC。这是我当前的设置。
//Application/core/MY_controller.php
class My_controller extends MX_controller
{
function __construct()
{
parent::__construct();
}
}
class dCommerce extends MY_controller
{
function __construct()
{
parent::__construct();
#Some of my code that shares the entire app, ex: login
}
}
现在在控制器中,我想要这样的东西//应用程序/控制器/sales_order.php
class Sales_order extends dCommerce extends dSales_Order
{
//dSales_Order have all my core APP logic
}
我知道PHP不可能实现多重继承,但是如何将逻辑与控制器分离呢?
Sales_order
将包含我所有基于框架的逻辑(取决于应用程序)ex、验证和核心类dSales_Order
将具有核心应用程序逻辑ex。保存、创建等
样品应该是这样的。。。
class dSales_Order
{
function save( $sales_order_id , $details )
{
//blah blah codes and APP logic, too much of math
//and then save to DB, this class will be framework Independed
//only pure php code
}
}
我怎样才能做到这一点?
我很抱歉英语不好。
我希望你的疑虑是OOPS和PHP相关的。我无法理解你的全部要求。
甚至我建议您使用名为traits
的新PHP概念或PHP 中的接口
好吧,我想我明白你现在的处境了;在交换了一些意见之后。
以下是我认为你应该做的:
- 建立对象列表(即销售、客户、员工等)
- 用核心PHP编写所有这些对象
- 然后,使用框架模型作为控制器和对象之间的附加层
- 您可以将对象文件移动到所需的任何框架中
- 您甚至可以通过REST API一次访问多个框架
希望这能有所帮助。