CodeIgniter Custom Controller My_controller usage Analysis _php instance

Source: Internet
Author: User
Tags codeigniter
The examples in this article describe the use of CodeIgniter custom controller My_controller. Share to everyone for your reference, as follows:

CodeIgniter all controllers must inherit the Ci_controller class, but the Ci_controller class is in the system directory and is not easy to modify. To facilitate some common processing, we typically 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 helper functions, common class libraries, and implement some common methods.

A common method? The public way?

See how these methods will be aware of a problem, if the method is public, whether it can be accessed through a browser. The answer is YES! this should not allow the user access to the method to let the user access. Let's set protected ...

Note : The write public method in Ci_controller is not accessible, and the framework restricts the way that methods in Ci_controller are accessed through the browser.

As the project progresses, there are more and more common methods in My_controller. If you want to increase the functionality of background management at this time, all the controllers still inherit My_controller, and many of these methods may not be applicable. 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 do this, the first is to differentiate between different public controller files, and the controller decides which public controller to inherit, and of course the public file is introduced here. There is also a way to maintain a property of an object, with different modules giving different objects to that property. Such as:

<?php if (! defined (' BasePath '))  exit (' No Direct script access allowed '); class My_controller extends Ci_controlle r{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 the file my_controller.php *//* location:./application/core/my_controller.php */

The controller calls the My_controller constructor and passes in the type value, loads the different class libraries according to the different type values, and then defines a uniform alias for the class to facilitate processing. The specific library can handle the common method of the module or the load common resource, which is 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 through the $this->helper object. In a closer look, you can find that the common classes of different modules are placed in the library, in the library or helper can use Get_intance to get the controller object, but each use needs to get the instance, relatively troublesome, if the model? Don't feel too good. Some of the common methods are related to business logic, and it is not appropriate to put them in the library.

Business logic does not seem to be a good place to implement the private method of the controller? Model?

First, summarize the above processing methods:

1, different modules can be loaded on demand and implement a custom common method, 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 public method is placed in the library, the call CI instance inconvenient.

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

More interested in CodeIgniter related content readers can view this site topic: "CodeIgniter Introductory Tutorial" and "CI (codeigniter) Framework Advanced Tutorial"

It is hoped that this article is helpful to 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.