Initially, we will start with a simple controller example that opens a home page with the navigation option back and forth to another page B, and similarly to the home page. We will do so while rendering some controller calculated data at the view.
This example doesn't use the database. This example will be built from the following CI framework component configuration, controller, and view files.
Let us define the default controller filename as controller/home_page.php
and the home page view as views/home_page_view.php
.
Let us assume the URI to the project root is http://mydomain.com/myproject
.
The controller file /home_page.php
will prepare some data to be shown in the view and will let the user navigate to page B and similarly back to the home page.
The helpers used are provided with the sample source code provided with this book.
The following is the code:
<?php class Home_page extends CI_Controller { function __construct() { parent::__construct(); $this->load->helper ('validators_helper'), $this->load->helper ('dates_time_helper'), } public function index() { $data = array (); $data ['email'] = $email = "[email protected]"; // validators_helpercalls $data ['email_valid'] = isValidEmail($email); $data ['url'] = $url = "http://cnn.com"; $data ['url_valid'] = isValidURL($url); $data ['url_exist'] = isURLExists($url); $this->load->view('home_page_view', $data); } publicfunction page_b () { $data = array (); $myqsl_date = "1970-01-01"; // dates_time_helper calls $data ['since'] = ui_date ($myqsl_date); $data ['past'] = getAgeAccurate ($myqsl_date, $percision = 2); $this->load->view ('page_b_view', $data); } } // End controller
The controller file will prepare the current date and time to be shown in the home page views/home_page_view.php
view.
<!DOCTYPE html> <meta content="text/html; charset=utf-8"/> <?PHP /* data from controller $email, $email_valid, $url, $url_valid , $url_exist */ $validation_text = ($email_valid) ? "Is Valid ": "Is Not Valid!"; $validation_url = ($url_valid) ? "Is Valid ": "Is Not Valid!"; $exist_url = ($url_exist) ? "Exist ": "Not exist!"; ?> <body style="text-align: left; color: blue;"> <H1>Main Page</H1> <HR></HR> <div style = "float: left"> The Email: <? = $email; ?><? = $validation_text; ?> </div> <div style = "clear: both;"></div> <HR></HR> <div> The url: <? = $url; ?><? = $validation_url; ?> and <? = $exist_url; ?> <? = anchor ($url, '[visit the url]', array ("target" => "_blank", "title" => "opens a new Tab")); ?> </div> <div style = "clear: both;"></div> <HR></HR> <?php echo anchor ('home_page/page_b', 'Navigate me to page B') ?> </body> </html>
The controller has a page_b()
method to render the following view file. It will prepare the parameters $since
and $past
for this page to be used inline in the rendered view page_b_view,
as follows:
The view file is views/page_b_view.php
.
<!DOCTYPE html> <meta content="text/html; charset=utf-8" /> <?PHP /* data from controller $since, $past */ ?> <body style = "text-align: left; color: blue;"> <H1>Page B</H1> <HR></HR> <div style = "float: left"> <!We render the provided controller parameters $since & $past> Since: <? = $since; ?> past<? = $past; ?> years</div> <div style = "clear: both;"></div> <HR></HR> <?php echo anchor ('home_page', 'Back to Home Page') ?> </body> </html>
Initially, we shall define at application/config/routes.php the default controller to be called.
For example, $route['default_controller'] = "home_page";.
So that in case, you will issue URI of the project root in the browser, lets say:
http://mydomain.com/myproject
http://mydomain.com/myproject/home_page will be called
.
Optionally we can configure CI to eliminate the need to use index.php
as part of the URI path to call our CI project controller/s (Refer for information, to Chapter 2, Configurations and Naming Conventions).
3.148.103.210