extending codeigniter controllers and separate business logic

804 views Asked by At

How can i separate my business logic away from my controllers in Codeigniter. I am using HMVC with Phills template library. Here is my current setup.

//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
  }
}

Now in controller i want something like this //Application/controllers/sales_order.php

class Sales_order extends dCommerce extends dSales_Order
{ 
  //dSales_Order have all my core APP logic
}

i know that multiple inheritance is not possible with PHP, However how can i separate the logic from controllers ?

Sales_order will contain all my framework based logic (depends app) ex, validation and the core class dSales_Order will have the core app logic ex. Save,Create etc

A sample would be like this ...

    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 
      }
   }

How can i achieve this ?

2

There are 2 answers

0
Praburam S On

I am sorry for my bad English.

I hope your doubts is an OOPS and PHP related. I cannot understand your full requirements.

Even I recommend you to go with new PHP concept called traits or interfaces in PHP

2
om_deshpande On

Ok, i think i understand where you're coming from now; after exchanging a few comments.

Here is what i believe you should do:

  • Build a list of objects (i.e. sales,customer,employee etc.).
  • Code all these objects in core PHP.
  • Then, use the framework models as an additional layer in between the controller and your objects.
  • You can move your objects files to any framework that you want.
  • You can even make accessible via a REST API to multiple frameworks at once.

Hope this helps.