CodeIgniter controller-business logic instance analysis-php instance

Source: Internet
Author: User
Tags codeigniter
This article mainly introduces how to use the business logic of CodeIgniter controller, and analyzes the related concepts and specific usage of the business logic in the form of instances, for more information about the business logic of CodeIgniter controller, see the following example. We will share this with you for your reference. The details are as follows:

We have analyzed the distribution of public controllers by module to facilitate control over specific modules. The specific implementation classes are placed in the library. Is it appropriate to put it in the library? And where should we put more business logic in the controller?

First, let's talk about the understanding of Several folders in CI.

Helpers and libraries: stores a series of helper functions and helper classes to assist controllers and implement business logic functions. The methods in them should try to avoid dependency with CI, and the tighter the dependency, the more difficult it is to reuse. Take sending an email as an example. Many parameters remain unchanged when sending an email, such as encoding, protocol, and port. We may configure these parameters in config, then, the library encapsulates a mail sending class and reads these parameters after obtaining the CI instance. In this case, the dependency with the CI instance appears, and the class can only be used in the CI framework. If other systems are to be used, they can only be rewritten, but cannot be reused. What if the sending class only receives parameters and encapsulates the sending method? Therefore, we recommend that you simplify helpers and libraries as much as possible, and simplify your responsibilities.

Controllers: controller directory. The Controller is mainly used to take over the program and act as a connection. Generally, we write the business logic in the action. However, as the business becomes complex, the action Code will become increasingly bloated and difficult to maintain.

Models: Model directory. The main responsibility of the CI model is to deal with databases and obtain data. The business logic is often put in the model, but the business logic and model are actually two things. The model only obtains data, and the business logic may combine the data according to the business needs. There may be many combination methods, which makes it difficult to maintain the model and is not conducive to reuse. In this example, the data is cached according to certain conditions, and the process of obtaining data and caching results is written in the same method, however, when the same data needs to be cached in another form, it is found that the method for obtaining data cannot be reused.

Third_party: third-party class library directory. Do not use a class library directly after obtaining it. You can encapsulate it in the library to make it more suitable for the system, and the difficulty of using it will also be reduced.

It can be found that each folder has its own responsibilities, and each module has its own home and has its own functions. What should I do with the business logic?

In this case, we should set up a unique directory for storing the business logic and name it service. The Controller is mainly responsible for receiving parameters and calling services and services to call models.

Let's take a look at how to implement it:

We can rewrite MY_Load, add the service method, and directly use

The Code is as follows:

$ This-> load-> service ('user _ Service ');

.
However, many business logic needs to obtain the CI instance. Here we can refer to the model method. The core creates a MY_Service, and all other services inherit this class, so that the usage of the service is the same as that in 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 is to have a layer to process the business logic, and both java have this layer. As I became familiar with CI, I found that this layer is needed to free up controllers and models. There are still many similar practices. If there are many places in the system that need to use web services or cache, you can also put them in a folder to facilitate management.

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.