Displaying Static Pages Using CodeIgniter part 1
Recap:In CodeIgniter to open a page, what you type on the address bar or the URL bar is something like:
http://mycodeignitersite.com/index.php/pages/about
Which is quite different even for usual PHP driven website. In CodeIgniter, the pattern is: http://[domainname]/index.php/[controller-class]/[method]/[arguements]
In my previous notes [Passing Paramaters In CodeIgniter] my example was http://localhost/index.php/hello/you
where:
localhost - is my domain since I'm testing this from my own laptop
hello - is my controller class which is saved in the controller folder
you - is my view method which is in the view folder.
[Note: index.php would always be there unless we tweak something on some configurations. - I would appreciate some help on how to remove index.php from a CodeIgniter 2.1.]
For this installment of my CodeIgniter tutorial notes, I will be creating a couple of static pages with a header and a footer templates which we will save on the views folder.
For my header template (header.php) the code is:
<html>
<head>
<title><?php echo $title ?> - CodeIgniter 2 Tutorial Notes </title>
</head>
<body>
<header style=" background-color: #FFD324 ">
<h1>Technotes by John CodeIgniter 2 Tutorial Notes</h1>
</header>
for my footer template (footer.php) the code is:
<strong>© 2011</strong>
</body>
</html>
As you may I have noticed I threw in a couple of html5 elements.
The Controller.
If we follow the conventional way of loading the pages in CodeIgniter, the url would be: http://localhost/pages/home and http://localhost/pages/about.
With this in mind the code in the controller page (pages.php) would be:
<?php
class Pages extends CI_Controller{
var $title;
public function home()
{
$this->title="codeigniter tutorial notes";
$data['title']=$this->title;
$this->load->view('templates/header', $data);
$this->load->view('pages/home', $data);
$this->load->view('templates/footer', $data);
}
public function about()
{
$this->title="About codeigniter tutorial notes";
$data['title']=$this->title;
$this->load->view('templates/header', $data);
$this->load->view('pages/about', $data);
$this->load->view('templates/footer', $data);
}
}
?>
As you can see, the header and footer templates are loaded along with the home and about pages. The title of the pages is the only parameter being passed along here.
This one works, but as you can see, the contents of both home and about functions are the same. Also, what if our site have five pages? or ten? or more?
We will see that on my next CodeIgniter TechNotes.
References:
http://codeigniter.com/user_guide/tutorial/static_pages.html
Comments
Post a Comment