Creating Views
We’re going to create a couple of templates to wrap our views with to keep our code DRY. Create a directory named ‘templates’ in your ‘application/views/’ directory and within that new ‘templates’ directory create two files; header.php and footer.php.
Type the following within header.php:
1 2 3 4 5 6 | <html> <head> <title><?php echo $title ?> - Up and Running With CodeIgniter</title> </head> <body> <h1>Up and Running With CodeIgniter</h1> |
We’ll be passing a title variable through our controller to dynamically populate the page title.
Type the following within footer.php:
1 2 | <strong>© 2014</strong> </html> |
We’re going to be using these template files as a dynamic header and footer for each of our pages views.
Next, we need to create a directory within the views directory called page. So now we should have two directories inside the ‘/views’ directory, ‘/page’ and ‘/templates’.
We’re going to create a home page and an about page inside the ‘/page’ directory.
Create home.php within ‘/page’ and write the following:
1 | <h1>This is home.</h1> |
Do the same for about.php:
1 | <h1>This is about.</h1> |
Now we have two pages inside ‘/page’ that only consist of one h1 tag. We also have two template files within ‘/templates’. For the purpose of this tutorial, we’re just going to leave these files as our views and move onto the controller and routes to demonstrate how to display these views in our browser.
Creating A Controller
CodeIgniter is an MVC framework, which means it uses the Model:View:Controller architecture. We’re going to create a Controller to delegate which Views are displayed when a user navigates through our URI structure. It’s best practice in CodeIgniter to use the same name of your directory within views as the name for your corresponding controller so we’re going to name this controller ‘Page’.
Navigate to the ‘application/controllers/’ directory and create a new file called page.php, this will be our controller for static page requests. Write the following within page.php:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?php class Page extends CI_Controller { public function view($page = 'home') { if ( !file_exists('application/views/page/'.$page.'.php')) { show_404(); } $data['title'] = ucfirst($page); $this->load->view('templates/header', $data); $this->load->view('page/'.$page, $data); $this->load->view('templates/footer', $data); } } |
Let’s go over the code above.
First, we’re declaring our Page class and we want to extend the class CI_Controller so that we have access to all of CI_Controller’s methods.
Our function view takes a parameter $page and we set that default parameter to home. This parameter will be passed in through our route definition as part of the URI request.
Next, we check to see if $page has a corresponding file name within our ‘/page/’ directory. If it doesn’t we call the show_404() method. If there is a corresponding file for the request we display that file by first passing in our title data in the form of an array. Then, we load our header, corresponding page, and our footer views to dynamically create a full page view. CodeIgniter will append each view to the previous view if multiple views are called within a controller. Notice how we’re passing in the array$data as our second parameter in the method view. This is necessary to populate the title tag of each page as that information is stored within the $data array.
Configuring our App Routes
Open up /application/config/routes.php in your IDE and you will notice that there are two routes already specified within the file.
Delete the default 404 route definition as we have programmatically handled our 404 errors in ourPage controller.
Next we’re going to change our default controller to this:
1 | $route['default_controller'] = 'page/view'; |
That will select our Page controller as the default when we navigate to our app’s url.
Next we need to define a route for our existing pages as well as any other pages we might create in the future by writing the following:
1 | $route['(:any)'] = 'page/view/$1'; |
Above, we’re using a special CodeIgniter string to define this URI route as anything. This specific part of the URI is what get’s passed into our controller as the $page parameter.
Now our application should resolve in the browser.
In order to get to the about page you must type in index.php/about because this is the wayCodeIgniter is built to handle URIs. There is a way to fix this with apache mod rewrites.
Rewrites
The last step in this process is creating our rewrites within .htaccess to make our URIs pretty. We can do this by opening up the .htaccess file and then typing the following:
1 2 3 4 | RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule .* index.php/$0 [PT,L] |
Now if we save and reload our page, we should be able to navigate to /about and see our pages dynamically generate with our header and footer templates along with the data we passed in as title.
Now Setup base url:
- Go to ‘application\config\config.php’
- Set $config[‘base_url’] = ”; to $config[‘base_url’] = ‘http://localhost/CodeIgniter’;
Source: http://www.programminghelp.com/php/get-running-codeigniter/