Welcome Guest, Not a member yet? Register   Sign In
Using libraries alongside models
#10

(This post was last modified: 04-30-2015, 05:13 AM by RWCH.)

I did not finish my last reply....

So my suggestion is not to have 3 layers (Model, Controller, View), but 4: Model, Service Layer, Controller, View.

In your controller, you access a service class like this:

PHP Code:
$data['blogs'] = blogService->GetBlogs('2015-04-01''2015--04-07');
$this->load->view('blogs_view'$data); 

In your service class (named "BlogService" here) you access your models like this:

PHP Code:
function GetBlogs(DatetIme fromDateDateTime endDate)
{
 
$blogs = array();

 
$homeData $this->_homeModel->GetHomeData(); // Get some data from a model
 
$personData $this->_personModel->getPersonData(); // Get some data form another model
 
 // Do all the things you like (busines logic) with the data you retrieved from the different models...
 
return $blogs 


In your models you access your data source (probably a database)

Probably you got the picture by now. But how to create this service layer? Controllers, Models and views are supported by the Codeigniter framework, but the service layer is not... so you have to extend it.
If possible you do not want to modify the CodeIgniter core libraries (anything in the system folder). I was able to create such a layer for CodeIgniter 2.

This is what I did:

Create a class MyService and place it in folder application/core:

PHP Code:
<?php
 
if (! defined('BASEPATH'))
 
   exit('Geen directe toegang tot het script toegestaan.');
 
class 
MyService
{
 
   protected $_CI null;
 
   protected $_lang null;
 
    
    function __construct
()
 
   {
 
       // Get the CI object.       
 
       $this->_CI = &get_instance();
 
        
        $this
->load = & $this->_CI->load//& load_class ( 'Loader', 'core' );     
 
        
        $this
->_CI->lang->load('Dutch');
 
       $this->_lang = & $this->_CI->lang    
    
}
}
 
?>

You can use this base class for all your services. Example of a service (placed in folder application/services):

PHP Code:
<?php
 
if (! defined('BASEPATH'))
 
   exit('Geen directe toegang tot het script toegestaan.');
 
require_once 
APPPATH 'core/MyService.php'//Necessary. If we want CodeIgniter to know how to find this class, we have to modify/extend the core.
 
class TeamService extends MyService
{
 
   private $_teamModel;
 
    
    function __construct
()
 
   {
 
       parent::__construct ();     
        $this
->load->model('TeamModel''_teamModel');
 
       $this->_teamModel = & $this->_CI->_teamModel;
 
        
    
}
 
 
   public function GetTeamNames($competitionId$divisionId
 
   {
 
       $result $this->_teamModel->GetTeamNames($competitionId$divisionId);
 
       return $result
 
   }
}
 
?>

Things to notice:

- The loading of the language in MyService is not really necessary. It is convenient to be able to have the language you are going to use available at this level. The following 2 lines take care of that:

       $this->_CI->lang->load('Dutch');
       $this->_lang = & $this->_CI->lang;
 

- Because the class cannot find the base class MyService, simply because the framework does not look for this type of class, you have to tell where to find it. That is what the require_once is for.  

- Models you have to load by hand. That is what the $this->load->model does.

- After that you assign it to a local class variable. That is what $this->_teamModel = & $this->_CI->_teamModel does.

So now you can access this class from every controller and you can access multiple models. Two other big advantages: You can totally separate your business logic (service layer -= business logic layer) and you prevent repetitive code. In the above example I can get teams in every controller where I need teams. Isn't that sweet?

I haven't migrated my code to Codeigniter 3 yet. It shouldn't be a problem to use my service layer again.

Hopefully this answered the question of the OP.
Reply


Messages In This Thread
Using libraries alongside models - by mdixon18 - 03-29-2015, 04:06 PM
RE: Using libraries alongside models - by RWCH - 04-03-2015, 07:17 AM
RE: Using libraries alongside models - by gadelat - 04-03-2015, 08:31 AM
RE: Using libraries alongside models - by RWCH - 04-07-2015, 03:44 AM
RE: Using libraries alongside models - by RWCH - 04-07-2015, 03:10 AM



Theme © iAndrew 2016 - Forum software by © MyBB