Using SEO-friendly URLs in CodeIgniter

At some point, you might want to alter how CodeIgniter handles the routing of URLs to controllers. By default, CodeIgniter splits a URL into several different parts. You obviously have the domain section of the URL (www.domain.com), but after that there are (usually, but not always) up to three more items, each separated by a forward slash. The first item is the controller, the second is the function (or method, if you want) in the controller, and the third is a parameter that you will pass to the function in the controller.

So, a standard URL in CodeIgniter might look like www.domain.com/users/edit/1. So, user number 1 is being edited using the edit function in the users controller--that seems simple enough and I'm sure you're familiar with it.

However, there may be times when you wish this to change. It is possible to alter what is displayed in the URL in the web browser's address bar to show something different from controller/function/data. It is possible to set up a rule in the config/routes.php file, which will map a URL to a controller, but hide this from the address bar; for example, you may have a controller named bulletin, which you wish to be displayed as news in the URL.

How to do it...

We're going to create one file and amend another file, by performing the following steps:

  1. Create the /path/to/codeigniter/application/controllers/shop.php file and add the following code to it:
    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'),
    
    class Shop extends CI_Controller {
      function __construct() {
        parent::__construct();
      }
    
      public function index() {
        echo 'Controller: ' . __CLASS__ . ', Method: ' . __FUNCTION__;
      }
    
      public function product() {
        echo 'Controller: ' . __CLASS__ . ', Method: ' . __FUNCTION__;
        echo '<br />';
        echo 'Product ID: ' . $this->uri->segment(2);
      }
    
      public function all() {
        echo 'Controller: ' . __CLASS__ . ', Method: ' . __FUNCTION__;
      }
    }
  2. Open the /path/to/codeigniter/config/routes.php file in your editor and amend accordingly (the changes are highlighted):
    $route['default_controller'] = "default_controller";
    $route['404_override'] = '';
    
    $route['item/(:any)'] = "shop/product";
    $route['item'] = "shop/all";
    

How it works...

Take a look at the following lines in the config/routes.php file:

$route['item/(:any)'] = "shop/product";
$route['item'] = "shop/all";

Now, think of them as being made up of a left and a right, in that before the = sign is left and anything after the = sign is right; the left maps to the right and the value in the left will be mapped to the value in the right.

In the preceding example, any URL whose controller name begins with item will be mapped to the shop controller. Let's look at each rule in turn:

By typing the name of the route into your browser (for example, http://www.your_web_site_name.com/item), the item command will cause CodeIgniter to call the shop controller, and within that controller the public function all() method. Because we defined it in the routes file, you will see this in your browser by typing item into the URL. CodeIgniter maps the requested URL to the path defined in the route rule, and (in our example) calls the shop controller, which using __CLASS__ and __FUNCTION__ will write the following output to the browser:

How it works...

Now, take a look at the URL in the browser address bar, it will still display the item, but the text on the screen says it's the shop controller that is being called. We have now mapped the URL to a controller and the user is none the wiser.

Now, let's look at the other route rule--:any. By typing the name of the route into your browser (for example, http://www.your_web_site_name.com/item/123456), the item/123456 command will cause CodeIgniter to call the shop controller and public function product() because we defined it in the routes file. In public function product(), we echo out not only the __CLASS__ and __FUNCTION__ names, but also the second segment of the URI:

$this->uri->segment(2);

Which, in this case, is the ID of the product (12356), so you will see the following screenshot in the browser:

How it works...

The key to this route is the (:any) flag in the route mapping rule; (:any) tells CodeIgniter that the second segment in the URI, whatever it is, should map to the product function of the shop controller. As we're the ones building the code, we will know that the second URI segment in the URL is a product ID, which we can use to query the database for the details of that product.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.129.100