CodeIgniter custom controller MY_Controller usage analysis

Source: Internet
Author: User
Tags codeigniter
This article mainly introduces the usage of the custom controller MY_Controller of CodeIgniter, and analyzes the definition and usage skills of the custom controller MY_Controller of CodeIgniter based on examples, for more information about how to use the CodeIgniter custom controller MY_Controller, see the example in this article. We will share this with you for your reference. The details are as follows:

All the controllers of Codeigniter must inherit the CI_Controller class, but the CI_Controller class is located in the system directory, which is not easy to modify. To facilitate some public processing, we usually create MY_Controller in core to inherit CI_Controller, so that all controllers in the project inherit MY_Controller.

So what does MY_Controller usually do?

All controllers inherit from MY_Controller. MY_Controller often loads public help functions, public class libraries, and implements some public methods.

Public method? Public method?

When you see these methods, you will be aware of a problem. if the method is public, can it be accessed through a browser. The answer is yes!This should not allow the user to access the method. Set protected....

Remarks: The public method written in CI_Controller will not be accessed, and the framework limits the access of methods in CI_Controller through a browser.

As the project progresses, there will be more and more public methods in MY_Controller. If you want to add the background management function at this time and all controllers still inherit from MY_Controller, many of these methods may not work. If some common methods required by the background are also written here, it will become messy.

How to differentiate controllers by module?

There are two ways to deal with it. The first is to distinguish between different public controller files. the controller determines which public controller to inherit. of course, public files must be introduced here. In addition, this mode can be maintained through an attribute of an object. different modules assign different objects to this attribute. For example:

<?php if ( ! defined('BASEPATH'))  exit('No direct script access allowed');class MY_Controller extends CI_Controller{ public function __construct($type = NULL) {   parent::__construct();   switch($type) {    case 'api' :     $this->load->library('api_helper', NULL, 'helper');     break;    case 'admin' :     $this->load->library('admin_helper', NULL, 'helper');      break;    default :     $this->load->library('app_helper', NULL, 'helper');      break   } }}/* End of file MY_Controller.php *//* Location: ./application/core/MY_Controller.php */

The controller calls the MY_Controller constructor and passes in the type value. Different Class libraries are loaded based on different type values, and a unified alias is defined for the class to facilitate processing. The specific library can process the public methods of the module or load the public resources, which is equivalent to a public class of the module. Of course, the processing method can also be directly controlled by the directory name or controller name in the route.

This avoids loading different files. you only need to call the method through the $ this-> helper object. Taking a closer look, we can find that the public classes of different modules are placed in the library, and the get_intance can be used in the library or helper to get the controller object, but every time you use it, you need to get the instance, which is relatively troublesome, what if it is a model? It doesn't feel good either. Some of the public methods are related to the business logic, and it is not appropriate to put them in the library.

The business logic does not seem to have a good place to implement. is the controller's private method? Model?

First, summarize the above solutions:

1. different modules can be loaded as needed and implement custom public methods. each module does not affect each other. If there are many common methods between modules, you can inherit a common class.

2. put public methods in the library, and it is inconvenient to call the CI instance.

3. if you do not like the call method of $ this-> herlper, you can let the controller inherit different public controllers. The idea is the same, but you may need to introduce files manually.

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.