Embedding your class structure in a CodeIgniter project

    Good day, comrades.

    In this post I’ll talk about how to circumvent the limitations that the framework imposes on the developer, while leaving the opportunity to take advantage of its functionality.

    Problem



    I am developing a resource for cross-posting on social networks. Initially, the product was intended only for Vkontakte and Facebook, and for working with the API one controller and one model was allocated, plus a model for working with cURL. While there was a need to work with only two social networks, this class structure of the project did not look depressing. But it was worth adding work with several more social services. networks, it became obvious that such a model leads to chaos and a complete mess both 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 an interface with common functionality for working with the API, We make a factory class and the only controller that will requisition 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 is based on CI models and libraries. Then I realized how wrong I was when I wrote in the coursework that CodeIgniter does not impose restrictions on the developer - even as it imposes. It is worth taking a little step in your decision beyond the framework of the MVC model, a problem arises - how to include this solution in the project. when I wrote in a term paper that CodeIgniter does not impose restrictions on the developer - even as it imposes. It is worth taking a little step in your decision beyond the framework of the MVC model, a problem arises - how to include this solution in the project. when I wrote in a term paper that CodeIgniter does not impose restrictions on the developer - even as it imposes. It is worth taking a little step in your decision beyond the framework of the MVC model, a problem arises - how to include this solution in the project.


    Decision



    At first glance, the obvious decision to extend in social classes. Networks and factories model CI_model and work as usual. But ideological infidelity of such an approach was also obvious.
    • Firstly: it makes it possible to load social models. networks separately from the factory class. So, in the future, not a sufficiently responsible outsourcer or an illegible beginner will take this opportunity and all the chaos and mess will return.
    • Secondly: classes simply should not contain the functionality of models.


    But instead of following the framework, I decided to experiment. First of all, it was decided to place an object of class CI_Loader in a separate field. But the solution was not very successful, as it required changes to the classes. Then we tried to place the whole CI model in a separate field. This is how the class diagram looked like this.
    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 you can 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 you need a little models, you can not select them in the collection but remember them as a separate field of the Framework class.

    And then the factory method is simply included in the desired controller or model (I made require in helpers so as not to spoil the appearance of the controller) and you're done!
    image

    Also popular now: