There are several different use cases for a CI user-defined controller. Commonly, the CI controller will handle initiating/rendering HTML pages, enable us to let the user navigate and view the different web application pages we defined. However, the controller may also provide other services, such as AJAX server-side controller, serving asynchronously the client-side browser requests, and commonly return back the JSON formatted data instead of a rendered view. The exact scope and usage of AJAX is not part of the CI framework, but it is very useful standard de-facto technology. To learn more about AJAX, please refer to AJAX (Asynchronous JavaScript and XML) on Wikipedia http://en.wikipedia.org.
The main usage categories for our controller in a CI framework are as follows:
http://mydomain.com/my_ci_project.
maincontroller
as the home page or default controller as follows under the project root:application/config/routes.php
. So that the default controller will be defined as follows:$route ['default_controller'] = "maincontroller";
http://mydomain.com/my_ci_project will be routed by the CI routes
to
http://mydomain.com/my_ci_project/maincontroller
For user navigation request to another page, we shall have in the rendered view, HTML anchors for navigating into another pages something like the following view code:
<?PHP echo anchor 'Navigate me to page B'); ?>
<script type="text/javascript"> function autocomplete(clue_val) { varurl = '<?php echo site_url(); ?>/AJAX_controller/autocomplete_name'; $AJAX ({type: "POST", url: url, data: {clue: clue_val}, dataType: "json", success: function(data) { // show the data of matching names } return; });
cron
requests: A very powerful CI usage we found is serving Linux cron
schedule requests defined in the Linux cron
(for more information about Linux cron
scheduling, please refer to http://en.wikipedia.org/wiki/Cron).We can find, within the DirectAdmin apache admin tool, a UI editor to define the scheduled crontab
repeatable actions we want the server to perform.
For each request, we will define the PHP processor path; for example, /user/local/bin/php
, as well as the PHP script to be executed; for example, /home/mysite.com/public_html/crontabs/ci_crontab.php
.
ci_crontab.php
can execute the CI controller method.
http://myCIproject/mycontroller/mymethod
will, for example, scan the database and update a table named sums_table
, which contains the number of rows in all the tables added together after every execution. Let us see an example of how to make that CI controller call from the PHP script.
In order to call a CI controller via an HTTP request, ci_crontab.php
will use the cURL service that will call the CI controller, similar to the way we issue it from a browser (cURL (Client URLs), http://php.net/manual/en/ref.curl.php).
Let's build /home/mysite.com/public_html/crontabs/ci_crontab.php
.
Linux crontab will call every defined action repeatedly.
The code of ci_crontab.php
will be something like the following code:
<?PHP function file_get_contents_curl($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_URL, $url); $data = curl_exec($ch); curl_close($ch); return $data; } $http_to_execut='http://myCIproject/mycontroller/mymethod'; $result = file_get_contents_curl ($http_to_execute);
The $result
will be the rendered output from the controller, mostly simple echoed messages such as Processed 127 entries
. Sure, we can log the result every time and append it to a log file of the action logs performed.
We just saw how we can use the CI controller to serve Linux cron services, which has a very powerful capability in many business cases.
18.227.10.45