The user-defined CI controller

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.

Extending the CI controller

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.

  • Loading resources of helpers, models, and libraries:
    • Those can be from the CI built-in repository, third-party developed, or self-developed. For more information on how to self-develop models, refer to Chapter 6, Models, on how to self-develop libraries, refer to Chapter 4, Libraries, and on how to self-develop helpers, refer to Chapter 5, Helpers.
    • The controller can load any of the external resources in the following fashion in any of its methods, commonly at the contractor and in case the resource is required in all the controllers via the 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'),
  • Adding public and private methods:
    • This approach is the common guideline of PHP OOP that you are expected to be familiar with (although elaboration on this can be found at http://php.net). The following is a simple example of how a public method calls a private method to get some data:
      public function get_something () {
        $some_data= $this->_get_it ();
        }
      private function _get_it () {
        return= 'hello';
        }
  • Using loaded resources:

    The loaded resources can be used after loading as follows:

    • Using loaded model methods:
      $status= $this->some_model->save_data($table, $row);$rows= $this->some_model->get_table($table_name);
    • Using loaded library methods:
      $another_data = $this->some_library->method($some_data);
    • Using loaded helpers:
      $your_ip =get_your_ip();  // myhtml_helper function
      // NOTE: a helper defines regular functions!
      
  • Calling a controller:
    • The controller is automatically instantiated by the CI core, and its methods are called via HTTP URIs. For more information, refer to Chapter 2, Configurations and Naming Conventions.
    • Ways to call a controller:

      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';

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.101.81