CodeIgniter Custom Controller My_controller usage analysis, CodeIgniter controller
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.
Articles you may be interested in:
- Case analysis of controller succession problem in CodeIgniter controllers
- 2 codeigniter file Batch Upload Controller example
- CodeIgniter Hook Usage Example detailed
- Example analysis of database.php usage of codeigniter configuration
- CodeIgniter Multi-language Implementation method detailed
- CI (CodeIgniter) Model Usage Example analysis
- CodeIgniter View Usage Considerations
- CodeIgniter Read and write separation implementation method detailed
- CI (CodeIgniter) Simple statistics access to the number of implementation methods
- Analysis of the business logic example of CodeIgniter controller
http://www.bkjia.com/PHPjc/1094761.html www.bkjia.com true http://www.bkjia.com/PHPjc/1094761.html techarticle CodeIgniter Custom Controller My_controller usage analysis, CodeIgniter Controller This example describes CodeIgniter custom controller My_controller usage. Share to everyone for your reference, ...