Each CI project must have one or more user-defined controllers in order to operate. The user-defined controllers are the starting point of any CI user interaction. Calling the controller and its methods can be done in several ways. The controller can be called via project root URI submission to a browser (the project default controller will be called), by issuing the user anchor from a rendered view, by a client-side AJAX request for actions (updating page selectors), or even by a crontab
(Linux known scheduler service) scheduled action executed repeatedly as a URI of a certain controller method.
We can see that the controller scope is a general manager of all the other project resources, such as models, views, helpers, and libraries, governing all to address execution requests from the user or a scheduled request.
Any application controller will be located under application/controller/
in the project
directory.
The controller can load other CI project code resources of libraries, models, and helpers so that they can be accessed directly by the rendered views. This means that, if a controller loaded a library, the rendered view PHP file can call the library function in the exact same way as the controller does. The following is the code resources that the controller can load:
application/helpers
: The helper/s are built-in CI third-party, or user-defined.application/models
: The models are most commonly user-defined for the specific database/s and tables of the specific project, extending the built-in CI model. Wrapping with CRUD service for specific defined database/s table/s, but also can be third-party (for example, data mapper extensions that can be used generically with any database).application/libraries
: The library can be built-in, third-party, or user-defined. The library is an Object Oriented PHP class-based service that can provide some reusable services related to a specific project, or across many projects. For example, as Flickr, Facebook, or LinkedIn wrapper API libraries. A good practice is to define in addition to the third-party libraries we may decide to use, our project oriented libraries to enhance our project simplicity, and maintainability.As we said, our application controller extends the built-in CI controller that is something like an abstract class in the development scope, so that in order to use the controller for our needs, we must build our controller extending the base class. We can extend the CI controller in several ways.
autoload.php
configuration file ( ) (refer to Chapter 3, Configurations and Naming Conventions for more information). However, for the best resource optimization to minimize the footprint and overhead even better, the resources will be loaded only on those controller methods that need their services to operate.The following are a few examples of how to load the mentioned resources:
$this->load->model('some_model'), $this->load->library('some_library', $keyed_array); $this->load->helper('some_helper'),
public function get_something () { $some_data= $this->_get_it (); } private function _get_it () { return= 'hello'; }
The loaded resources can be used after loading as follows:
$status= $this->some_model->save_data($table, $row);$rows= $this->some_model->get_table($table_name);
$another_data = $this->some_library->method($some_data);
$your_ip =get_your_ip(); // myhtml_helper function
// NOTE: a helper defines regular functions!
Calling only the contractor and later calling the index method, if defined as follows: $URI = "base_url().'/mycontroller'; mycontroller';
Calling the method of the mycontroller
class without parameters: $URI = "base_url.'/mycontroller/mymethod';
Calling the method of the mycontroller
class with two parameters, a and b: $URI = "base_url.'/mycontroller/mymethod/a/b';
3.145.101.81