Example analysis of CodeIgniter Controller's business logic example _php

Source: Internet
Author: User
Tags codeigniter

The business logic of CodeIgniter Controller is analyzed in this paper. Share to everyone for your reference, specific as follows:

The previous analysis of the public controller by module distribution, to facilitate the control of specific modules, and the specific implementation of the class is placed in the library. Is it appropriate to put it in the library? And where is the more business logic in the controller?

First of all, the understanding of several folders in CI

Helpers, Libraries: Store a series of auxiliary functions, auxiliary classes, to assist the Controller, business logic implementation functions. Their methods should try to avoid being dependent on CI, and the tighter the dependency, the harder it is to reuse. To send a message as an example, many of the parameters are unchanged, such as encoding, protocol, port, and so on, we may configure these parameters under Config, and then the library encapsulates a message sent by the class, and in which the CI instance is fetched after reading these parameters. There is a reliance on the CI instance, which can only be used in the CI framework, and can only be overridden for other systems, and is not intended for reuse. What if the sent class just receives the parameters and encapsulates the sending method? So, as far as possible to let helpers, libraries change simple, responsibility becomes unitary.

Controllers: Controller directory. The controller is mainly used to take over the program and play the role of connection. Typically, we write the business logic in action. But as the business becomes more complex, the action code becomes bloated and difficult to maintain.

models: Model directory. The primary responsibility of the CI model is to deal with the database and get the data. Most of the time, the business logic is in the model, but the business logic and the model are actually two kinds of things. The model just gets the data, the business logic may be to combine the data according to the business needs, the combination may have many kinds, put in the model will make the model difficult to maintain and not conducive to reuse. A case in point, the data on a certain condition cache, get data and cache results two processes written in the same method, but the same data need to do another form of caching, found that the method of obtaining data can not be reused.

Third_party: Third-party class library directory. To get a class library do not use directly, you can do a package in the library, so that it is more suitable for the system, other people to use the difficulty will also be reduced.

It can be found that each folder has its own responsibilities, each module has its own home, has its own functions. What about the business logic?

In this case, we should also give the business logic home, set up a unique directory to store the business logic, for the moment named service. The controller is primarily responsible for receiving parameters and calling Service,service to invoke the model, each layer doing its duty.

Here's how it's going to happen:

We can rewrite My_load, add service method, directly through

Copy Code code as follows:
$this->load->service (' User_service ');
To invoke.
But a lot of business logic needs to get CI instance, here can refer to the method of the model, core establishes a my_service, the other service inherits this class, so the usage of the child service is same as the controller.

Class My_service
{public
  function __construct ()
  {
    log_message (' Debug ', Service Class initialized) ;
  }
  function __get ($key)
  {
    $CI = & Get_instance ();
    return $CI-> $key
  }
}

In fact, the main idea or need to have a layer to deal with business logic, Java has this layer. With the constant familiarity with CI, it is found that this layer is needed for the purpose of liberating the controller and the model. And this similar practice there are many, if there are many places in the system need to use the Web service or cache, and so on, in fact, can also be in accordance with the above idea of a separate folder to deal with, facilitate management.

More readers interested in CodeIgniter-related content can view the site topics: "CodeIgniter Introductory Course" and "CI (CodeIgniter) Framework Advanced Course"

I hope this article will help you with the PHP program design based on CodeIgniter framework.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.