Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Custom front controller

Derek Jones edited this page · 9 revisions
Clone this wiki locally

Category:Help::TipsAndTricks | Category:Help::Controller

Introduction Almost everything from the framework is extendable but only a few people dare to touch the front controller. This tutorial shows you how to do this without having to touch the out-of-the-box font controller and has an example of what is possible.

The front controller People who already looked at the index.php file in the root directory see a file called CodeIgniter.php placed in the system/codeigniter directory is included. This file loads all the magic of the framework.

Because the file is included it's easy to direct it to another file and then the experimentation can start.

The front controller content The first thing you see in the CodeIgniter.php is the creation of the CI version constant.

Next you see the inclusion of the file Common.php. This file contains following functions :

  • is_really_writable : to check the directory properties especially on Windows
  • load_class : similar to the load->library method
  • get_config : includes the default config file
  • config_item : similar to the config->item method
  • show_error : outputs an error page
  • show_404 : outputs a page not found error page
  • log_message : adds message to log
  • _exception_handler : adds php errors to CI log file

The next file to be included is the Compat.php file. This is to make sure certain functions that the framework uses exist on every server configuration. Then the _execption_handler is set and the magic quotes are turned of.

Following this the loading of the libraries starts. Between the loading of the libraries the benchmark start marks are set, the pre-system hook and cache is called when they are present.

Then the base class is loaded. Everything is added to the base class from now on. Next the controller that is called via the url is included en called and everything is wrapped up.

The example I'm going to add the line numbers to give you a clue where to look but it's possible in your CodeIginter.php file the line numbers could be different.

First i'm going to make some changes that aren't necessary but improve the readability of the code.

On lines 169 and 170 you find following code


$class  = $RTR->fetch_class();
$method = $RTR->fetch_method();

Move this above the file_exists check for the controller, line 149, and add following code


$class_dir =  $RTR->fetch_directory();

This makes it possible to change a few lines to call the router functions a few times less. line 149

if ( ! file_exists(APPPATH.'controllers/'.$class_dir.$class.EXT))

line 154

include(APPPATH.'controllers/'.$class_dir.$class.EXT);

This also makes it possible to load a default view for the controller method, add after line 233 following code


// Call default view file if it exists
if(file_exists(APPPATH.'views/'.$class_dir.$class.'/'.$method.EXT))
{
    $CI->load->view($class_dir.$class.'/'.$method.EXT);
}

If you didn't add the $class_dir variable, the code after line 233 will be as follows


// Call default view file if it exists
if(file_exists(APPPATH.'views/'.$RTR->fetch_directory().$class.'/'.$method.EXT))
{
    $CI->load->view($RTR->fetch_directory().$class.'/'.$method.EXT);
}

For people who don't understand what the code actually does there is a response in the thread that inspired this tutorial by sophistry that does a run down of all the changes and the usage. I've added the changed file to the wiki for the lazy typers.

If you save the file in the application directory you can change the front controller path in the index.php file to

require_once APPPATH.'CodeIgniter'.EXT;

And then it's possible to let the framework do the loading of the view file if you use the same names as the controllers and methods.

Something went wrong with that request. Please try again.