Chapter 3. Controller Usage and Scope

This chapter covers the CI controller scope and the various controller usage categories with several code examples of web applications. The controllers are the front-line decision makers of how to process or route a request and how to respond to actions, such as a rendered view sending back to the browser as an HTML page, an AJAX response to let the current requesting page update certain selector areas by the response, or even just to update the database seamlessly. The controllers use the available models, helpers, libraries, and views to respond to the caller, be it a web browser URL or a cron process issuing the cURL types POST/GET requests automatically.

The CI built-in controller behaves like an abstract class in the project scope so any of our project controllers must be extensions of a built-in CI controller. Our developed controller will inherit the CI built-in controller capabilities and built-in resources, such as auto-loaded helpers, libraries, and models, and let us code any specific services as controller methods to address our project requirements, and rendering the needs of the view.

As mentioned before, the controller is part of the MVC development framework that operates with models, and applies business logic, which most commonly renders a view back to the client web browser to proceed with user interaction.

The web user refers, via a browser, to a URL. In CI, the view is implemented as a call to a CI controller method. The CI controller method processes the browser request and sends back a rendered view that becomes a visualized web page to the browsing user.

The web page received from the CI controller is referred to as the rendered controller view; it may include anchors and buttons for the user to continue the interaction with the controller. When the user clicks on an anchor in the browser, a call to a controller is made (the anchor makes an HTTP request to activate a controller call). In case the user issue a event such as Button clicking, operate scroll bars, and so on so that this user event will trigger an action using jQuery to activate, for example a jQuery callback function. The jQuery callback function may issue a call to another rendering controller, such as :

$(location).attr('href',controller_url_to_call);

Another option is to activate an asynchronous AJAX calls to an AJAX controller. Such a controller function handles the AJAX request that we will discuss later on. When AJAX calls returned from the server , their returned data may be used by client-side JavaScript to, update certain page regions/ (HTML selector/s) of the web page, for example, when typing a search string and issuing a search button, the AJAX response will provide the search results to be visualized in the same web page or will move to another page.

More on controllers in general can be found at http://en.wikipedia.org/wiki/Model-view-controller.

The following are the chapter topics and subtopics we will cover:

  • Scope of the CI controller
  • Usage categories:
    • Rendering views
    • Controllers serving browser AJAX requests
    • Controllers serving Linux scheduled cron execution requests
  • CI controller expansion and usage( )(Refer to Chapter 2, Configurations and Naming Conventions, for more)
    • Loading resources of models, helpers, and libraries
    • Using loaded resources
    • Calling CI controller methods
  • Example 1: the default home page controller
  • Example 2: sending e-mails with attachments
  • Example 3: admin and regular user log in

We will begin by briefly reviewing the scope and use cases of the application controller in the CI framework and how we can use them for our project requirement needs.

Scope of the CI controller

The CI controller is the hub and brain of the CI MVC that handles the HTTP requests, such as those from the browser, and operates with other CI resources to respond. The CI controller scope is described in the following figure and operates with other CI resources to respond to the requests:

Scope of the CI controller

The general flow with the CI controller is shown in the preceding figure. The user's browser sends the HTTP request to the URL of the CI project. Initially, the requested URL is processed via the routes based on the routes configuration /conf/routes.php. For more information, refer to Chapter 2, Configurations and Naming Conventions. The specific CI controller is instantiated and the specific method is called. The CI method may be assisted by any of the project resources, such as models, libraries, and helpers for operations (business logic, and database queries). The CI controller generally uses a view that defines a web page to be responded to via an HTTP response.

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

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