Codeigniter 2.0: Extending the Controller class

Extending Codeigniter core classes is often useful for adding functionality or changing an existing function without altering the frameworks core code. We’ve already done this once with the router class for search engine friendly URLs and this time we’ll be doing it with the CI_Controller class so that we can set the referring controller every request although you could do anything here. E.g setting a userdata variable.

The first step is to create a file called MY_Controller.php in the application/core/ folder. Remember that capitalization is important. The file should contain the following code: 

<?php
class MY_Controller extends CI_Controller {

	function __construct ()
	{
		parent::__construct();
		$this->session->set_flashdata('referrer', uri_string());
	}

}

The above code stores the method and class plus any other segments in a flashdata variable called referrer.

In order to use any code you put in your MY_Controller class you need to make sure your controllers extend MY_Controller instead of CI_Controller (See the example below). The code will be ran and you can access it in the normal way.

Example Controller – Default welcome.php: 

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends MY_Controller {

	public function index()
	{
		echo "Referrer: ".$this->session->flashdata('referrer');
		echo anchor(uri_string(), 'clicky');
		$this->load->view('welcome_message');
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */

Using hyphens instead of underscores in CodeIgniter

When I’m building a CodeIgniter application there are several important things that I need to think about whilst putting the whole project together. Two of these things are Accessibility and Search Engine Optimisation (SEO) and in particular for this article the URLs I use to link it all together.


First, let’s take a look at a standard CodeIgniter URL.

http://www.example.com/example/hello_world

The first segment (example) normally refers to the controller that you are accessing. The second segment (hello_world) refers to the function in that controller that you want to execute in order to build the content that will be displayed. As you can see here our function is called hello_world() and uses an underscore to separate two words within the name. In terms of SEO and accessibility it is often better to use a hyphen (-) instead of an underscore (_) within an URL and one key reason for using the latter is that many applications and Search Engine Spiders such as Google interpret hyphens as spaces whilst underscores are often left as they are.

Using Codeigniter it is possible to convert the underscores in function names in to hyphens for URLs so that http://www.example.com/example/hello-world actually calls the function hello_world within our example controller. In order to do this you need to extend and alter the Codeigniter core Router class so that it’ll replace the hyphens within the URL with underscores during runtime. In order to achieve this you can use the code below.

Step 1. Create a custom Router file in ./application/core/ called MY_Router.php. If the core folder does not exist in your installation then create it.

Step 2. Insert the following code in to the file, save it and you’re done!

<?php
class MY_Router extends CI_Router {

	function _set_request ($seg = array())
	{
		// The str_replace() below goes through all our segments
		// and replaces the hyphens with underscores making it
		// possible to use hyphens in controllers, folder names and
		// function names
		parent::_set_request(str_replace(‘-‘, ‘_’, $seg));
	}

}