Categories
Posted in: CodeIgniter

Routes with CodeIgniter

Using Routes with CodeIgniter helps us with the URL structure of our site. Below you will find out exactly what is a Route, and how to implement them in you CodeIgniter application. The default url structure is site.com/index.php/controller/function/ID as discribed by CodeIgniters documentation. We will make them like site.com/about in this article.

This article assumes you know the basics of MVC, and CodeIgniter framework.

First create a new controller in you application/controllers and name it something like pages.php

Place the following inside your new php file.

<?php class Main extends Controller {
     public function __construct(){
         parent::__construct();
     }
     public function index(){
         echo "Homepage";
    }
     public function about(){
         $this->load->view('about');
}

public function contact(){
$this->load->view('contact');
}

public function services(){
$this->load->view('services');
}
}
?>

What we are doing here is creating the starting point for each new page, and telling it to load the the view.

Next create a new php file for our view in application/views ultimately there will be a view for each page. Name this file homepage.php It is also wise to set up a header.php and footer.php in the views folder. This helps us to reuse our code for each page and is also good practice.

<html>
<head>
<title><?php echo $title; ?></title>
</head>

<body>
    <h1><?php echo $altText ?></h1>
        <img id="hero"
             src="<?php echo $hero?>"
             alt="<?php echo $altText ?>" />
	<p>this is some information about the project.
You should update the controller with the information that you would like
 to appear here.</p>
</body>
</html>

You could use your own HTML if you wish to do so.

Do this for each view that we have listed in our controller file, contact, about, services, etc.

Next we are going to update our routes.php file located in application/config/routes.php

You will notice the default route of $route[‘default_controller’] = “welcome”; you can comment this line out so we can remember the syntax of future routes.

Below that add this line. You don’t have to place them in any special order although it does help to keep them in order, at least it does for me anyhow.

$route['homepage'] = "pages/homepage";

Continue adding the others underneath.

$route['homepage'] = "pages/about";
$route['homepage'] = "pages/contact";
$route['homepage'] = "pages/services";

So were finished right? Not just yet my friend. We still have one more thing to do. If you go to you site and test what we have done thus far. You will see you still have that pesky index.php in your URL string. We’re going to fix that now.

In the root of your site I.E. public_html folder create a .htaccess file if you don’t have one already, and insert the following code into it.

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index.php|css|js|images|robots.txt)
# this line adds index.php to the URL so CodeIgniter can process it.
RewriteRule ^(.*)$ index.php?$1 [L]

Now you can link to and view you site at site.com/about

A few tricks on .htaccess

2 thoughts on “Routes with CodeIgniter

  1. I actually seem to go along with every little thing that was written inside
    “How to use Routes with CodeIgniter | Matrix Web Designers”.
    I am grateful for all the facts.Thanks,Candice

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.