CodeIgniter custom controller MY_Controller usage analysis, codeigniter controller_PHP tutorial

WBOY
Release: 2016-07-12 09:00:06
Original
1035 people have browsed it

CodeIgniter custom controller MY_Controller usage analysis, codeigniter controller

The example in this article describes the usage of CodeIgniter custom controller MY_Controller. Share it with everyone for your reference, the details are as follows:

All controllers in Codeigniter must inherit the CI_Controller class, but the CI_Controller class is located in the system directory and is not easy to modify. In order to facilitate some common processing, we usually create MY_Controller under 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 MY_Controller, which often loads some public helper functions, public class libraries, and implements some public methods.

Public method? Public method?

When you see these methods, you will realize a problem. If the method is public, can it be accessed through the browser? The answer is yes! In this way, users have access to a method that should not be accessible to users. Then set protected . . .

Remarks: Public methods written in CI_Controller will not be accessed. The framework limits access to methods in CI_Controller through the browser.

As the project continues to progress, there will be more and more public methods in MY_Controller. If you want to add background management functions at this time, and all controllers still inherit MY_Controller, many of the methods may not be applicable. If some public methods needed by the backend are also written here, it will become confusing.

How to distinguish different controllers by module?

There are two processing methods. The first one is to distinguish through different public controller files. The controller decides which public controller to inherit. Of course, the public file must be introduced here. Another way is to maintain it through an attribute of the object, and different modules assign this attribute to different objects. Such as:

<&#63;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 */

Copy after login

The controller calls the MY_Controller constructor and passes in the type value. Different class libraries will be loaded according to different type values, and then a unified alias is defined for the class to facilitate processing. The specific library can handle 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 controlled directly through the directory name or controller name in the routing, etc.

This avoids loading different files. When calling the method, you only need to call it through the $this->helper object. After taking a closer look, you can find that the public classes of different modules are placed in the library. You can use get_intance to obtain the controller object in the library or helper, but you need to obtain the instance every time you use it, which is relatively troublesome. What if it is a model? It doesn't feel good either. Some of the public methods are related to business logic, and it feels inappropriate to put them in the library.

There seems to be no good place to implement business logic, private methods of the controller? Model?

Let’s first summarize the above processing methods:

1. Different modules can be loaded on demand and customized public methods can be implemented, and each module does not affect each other. If there are many common methods between modules, you can also inherit a common class.

2. Public methods are placed in the library, and it is inconvenient to call CI instances.

3. If you don’t like the calling method of $this->herlper, you can let the controller inherit different public controllers. The idea is the same, but you may need to manually introduce the file.

Readers who are interested in more CodeIgniter related content can check out the special topics on this site: "codeigniter introductory tutorial" and "CI (CodeIgniter) framework advanced tutorial"

I hope this article will be helpful to everyone’s PHP program design based on the CodeIgniter framework.

Articles you may be interested in:

  • Codeigniter controller controller inheritance problem example analysis
  • 2 Codeigniter file batch upload controller writing examples
  • CodeIgniter Detailed explanation of hook usage examples
  • CodeIgniter configuration database.php usage example analysis
  • Detailed explanation of CodeIgniter multi-language implementation method
  • CI (CodeIgniter) model usage example analysis
  • Notes on using the CodeIgniter view
  • Detailed explanation of the codeIgniter read-write separation implementation method
  • CI (CodeIgniter) simple statistical visitor number implementation method
  • CodeIgniter controller business logic example analysis

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1094761.htmlTechArticleCodeIgniter custom controller MY_Controller usage analysis, codeigniter controller This article describes the usage of CodeIgniter custom controller MY_Controller. Sharing it with everyone for your reference,...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template