Service Level in Codeigniter 2 with Doctrine 2

hope some of you can give me a hand with this.

Im working on a project using Codeigniter 2 and Doctrine 2, everything is working fine, but I have some "sanity" problems that I would like to fix.

The main problem that I am facing now is the persistence of entities. In normal MVC, the resistance should be in the model, but now that I have only entities and repositories, and I do not have the so-called "models", I put all this code into the controller, making them huge and intimidating :(

I read in some places that the best approach for this is to have a “Service” layer between the controller and the objects, but I have not found a good way to do this in Codeigniter due to the tight MVC classic.

So, I ask for advice on how to understand this. Do any of you guys have the same problems?

+4
source share
2 answers

I found a solution for my problem, hope this works for some of you.

I use the integration of Joel Verhagen Codeigniter 2 and Doctrine 2, you can read his article for more details http://www.joelverhagen.com/blog/2011/05/setting-up-codeigniter-2-with-doctrine-2- the-right-way / "

In simple words, I use Codeigniter Models as a service level. This was the cleanest approach I could find, mainly because all the “postings” are already done by Codeigniter, so I didn't have to do anything: D.

I had to make some changes to the folder structure of the Joel implementation, so I can use CI models and still use its Doctrine code. So I moved everything from the “model” folder to a new folder called “entities” (I know that this may not be the best name, but it works: P). Then I changed all the links to the new folder and checked that everything worked.

What is it, now I have my "service layer", and my code is much cleaner.

If any of you need help, please feel free to ask me.

+1
source

He returned to the same boat. Ended up without using Doctrine ORM, but in principle you are right - you need a “service level” for everything that is not directly modeled using Doctrine objects and repositories.

How do I do this by creating a folder with names in / application / for my project code. Then I used the Doctrine Common class loader to recognize this folder as a namespace. For example, /application/Acme/Authentication.php contains:

namespace Acme; class Authentication { //Do Doctrine queries in various methods here } 

The Doctrine class loader class uses SPL (spl_autoload_register or something else). This means that you can make full use of the PHP 5.3 namespaces. Then you have all the fun tests and intimidation injection dependencies to access the dbal doctrine within this level of service. Then your controllers will use this "service level". As I said, in my case I decided not to use Doctrine ORM, so I use the CodeIgniters ActiveRecord database classes inside my “service level”. Instead of using $ this-> CI = & get_instance () ... I provide access to the database in the constructors using the DI container.

For example, in my authorization / login controller action, I can have

 function login() { $user = $_POST['u']; $pass = $_POST['p']; $auth = new Acme\Authentication($this->db); //or use a DI container $user = $auth->authenticate($user, $pass); .... } 
0
source

Source: https://habr.com/ru/post/1439223/


All Articles