Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Business Logic in CodeIgniter Controller

2025-03-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article shares with you the content of a sample analysis of business logic in a CodeIgniter controller. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Let's first talk about the understanding of several folders in CI.

Helpers, libraries: store a series of auxiliary functions and auxiliary classes to assist the controller and business logic to realize the function. The methods in them should avoid relying on CI as much as possible, and the tighter the dependency is, the harder it is to reuse. Take email sending as an example. Many parameters remain unchanged when sending email, such as encoding, protocol, port, etc. We may configure these parameters under config, and then library encapsulates a class sent by email, and reads these parameters after obtaining the CI instance in it. At this point, there is a dependency on CI instances, this class can only be used in the CI framework, other systems can only be used, can only be rewritten, does not achieve the purpose of reuse. What if the class that is sent simply receives parameters and encapsulates the sending method? So, as far as possible to make helpers, libraries become simple, the responsibility becomes single.

Controllers: controller directory. The controller is mainly used to take over the program and play a connecting role. Usually, we write the business logic in action. But as the business becomes more complex, the action code will become more and more bloated and difficult to maintain.

Models: model directory. The main responsibility of the CI model is to deal with the database and obtain data. In many cases, business logic is also put in the model, but business logic and model are actually two different things. The model only acquires data, and the business logic may combine the data according to business needs. There may be many ways of combination. Putting the model in the model will make the model difficult to maintain and not conducive to reuse. As an example, the data is cached according to certain conditions, and the two processes of obtaining data and caching results are written in the same method, but when the same data needs to be cached in another form, it is found that the method of obtaining data cannot be reused.

Third_party: third-party class library directory. Don't use it directly after you get a class library. You can encapsulate it in library to make it more suitable for the system, and it will be easier for others to use.

You can find that each folder has its own responsibilities, each module has its own home, and each has its own functions. What about the business logic?

In this case, we should also make a home for the business logic and establish a unique directory to store the business logic, temporarily named service. The controller is mainly responsible for receiving parameters and calling service,service to invoke the model, and each layer does its part.

Let's see how to do this:

We can rewrite MY_Load, add the service method, and go directly through the

$this- > load- > service ('user_service')

To call.

However, a lot of business logic needs to obtain CI instances. Here you can refer to the method of the model. Core creates a MY_Service, and other service inherits this class, so the usage in service is the same as 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 still needs to have a layer for dealing with business logic, which is found in java. With the increasing familiarity with CI, it is found that this layer is needed here to liberate the controller and model. There are many similar practices, if there are many places in the system that need to use web service or cache, in fact, you can also follow the above ideas to deal with in a separate folder, easy to manage.

Thank you for reading! This is the end of this article on "sample analysis of business logic in CodeIgniter controller". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report