CodeIgniter Custom Controller My_controller usage Analysis _php instance

Source: Internet
Author: User
Tags codeigniter

This example describes the CodeIgniter custom controller My_controller usage. Share to everyone for your reference, specific as follows:

CodeIgniter all controllers must inherit the Ci_controller class, but the Ci_controller class is in the system directory and is not easily modified. For the convenience of doing some common processing, we usually create my_controller under the core to inherit the Ci_controller, so that all the controllers in the project inherit My_controller.

So, what does My_controller usually do?

All controllers inherit My_controller, My_controller often load common help functions, common class libraries, and implement some common methods.

A common method? A public way?

Seeing these methods will make you aware of a problem that can be accessed through the browser if the method is public. The answer is YES! this should not allow users to access the method that the user has access to. That set protected ...

Note : The write public method in Ci_controller is not accessed, and the framework restricts access by the Ci_controller method through the browser.

As the project progresses, there will be more and more common methods in the My_controller. If you want to increase the functionality of background management at this time, all controllers still inherit My_controller, and many of these methods may not apply. If some of the common methods needed in the background are also written here, this will become confusing.

How do I differentiate different controllers by module?

There are two ways to deal with it, the first of which is to differentiate between the different public controller files, and the controller to decide which public controller to inherit, of course, to introduce common files here. It is also possible to maintain a property of an object by using a different module to give the attribute a different object. Such as:

<?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 invokes the My_controller constructor and passes in the type value, loads the different class libraries based on the different type values, and then defines a uniform alias for the class to facilitate processing. A specific library can handle the common methods of the module or the resources that are common to the load, equivalent to a common class of the module. Of course, the processing can also be directly through the directory name in the route or controller name to control and so on.

This avoids loading different files, and calls the method only by $this->helper object. In a closer look, you can find that the common classes of different modules are placed in the library, put in the library or helper can use Get_intance to get the controller object, but each use need to obtain an instance, relative trouble, if the model? It doesn't feel too good. Some of the common methods are related to the business logic, and the library doesn't feel right.

The business logic does not seem to have a good place to implement, the private method of the controller? Model?

Summarize the above processing method first:

1, different modules can be loaded on demand as well as the implementation of customized public methods, each module does not affect each other. If there are more common methods between the modules, you can also inherit a common class.

2. The common method is placed in the library, it is inconvenient to invoke CI instance.

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

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.

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.