📜 ⬆️ ⬇️

Embedding your class structure in a project on CodeIgniter

Good day, comrades.

In this post I will talk about how to get around the limitations that the framework imposes on the developer, while leaving behind an opportunity to take advantage of its functionality.

Problem


')
I am developing a resource for cross-posting to social networks. Initially, the product was intended only for Vkontakte and Facebook, and one controllers and one model were allocated to work with the API, plus a model for working with cURL. While there was a need to work with only two social networks, such a class structure of the project did not look depressing. But it was worth adding work with several social services. networks, it became obvious that this model leads to chaos and complete mess as on the side of working with the API and on the client side. What is the branch of 10 else if for viewing user data or 10 ajax requests for sending messages to social networks? It was decided to refactor all this horror, using the Factory pattern. Everything seemed simple: we describe the interface with the general functionality of working with API, we make the factory class and the only controller that will be requisitioning the factory class. But as soon as they began to transfer functionality to a new paradigm, it dawned on us. All work in the database, user data, logs and https rests on CI models and libraries. Then I realized how wrong I was when I wrote in the course that CodeIgniter does not impose any restrictions on the developer - it also imposes. It is worth a little step in your decision beyond the MVC model, the problem arises - how to include this solution in the project.


Decision



At first glance, the obvious solution zaeksendit in the classes of soc. networks and factories model CI_model and work as usual. But it was also obvious ideological infidelity and inflexibility of this approach.


But instead of going on about the framework, I decided to experiment. First of all, it was decided to place in a separate field an object of class CI_Loader. But the decision was not very successful, since it required making changes to the classes. Then we tried to make the whole CI model in a separate field. Something like this looked like a class diagram.
image

The Framework class is a regular CI model.
class Framework extends CI_Model { /** * * @var stdObject $model     */ public $model; /** * * @var stdObject $library     */ public $library; public __construct() { parent::__construct; $this->load->model('https'); $this->model->dx_auth = $this->dx_auth; $this->model->https = $this->https; $this->library->db = $this->db; } } 


And voila

 class ACSocial { /** * *@var Framework    */ public $framework; public function __construct() { $this->framework = new Framework(); ... 


Now it is possible to access models and libraries from any class that inherits ACSocial in an almost familiar way.
 $this->framework->model->db->get_where('users', array('id' => $id)); 

if the necessary models are a little, you can not select them in the collection but memorize them as a separate field of the Framework class.

And then the factory method is simply included in the desired controller or model (I took require into helpers so as not to spoil the appearance of the controller) and that's it!
image

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


All Articles