Skip to content

Custom front controller

World Wide Web Server edited this page Jul 4, 2012 · 9 revisions

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

[b]Introduction[/b] 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.

[b]The front controller[/b] 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.

[b]The front controller content[/b] 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.

[b]The example[/b] 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 [code] $class = $RTR->fetch_class(); $method = $RTR->fetch_method(); [/code] Move this above the file_exists check for the controller, line 149, and add following code [code] $class_dir = $RTR->fetch_directory(); [/code] This makes it possible to change a few lines to call the router functions a few times less. line 149 [code]if ( ! file_exists(APPPATH.'controllers/'.$class_dir.$class.EXT))[/code] line 154 [code]include(APPPATH.'controllers/'.$class_dir.$class.EXT);[/code] This also makes it possible to load a default view for the controller method, add after line 233 following code [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); } [/code] If you didn't add the $class_dir variable, the code after line 233 will be as follows [code] // 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); } [/code]

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 [url=http://codeigniter.com/forums/viewthread/80439/P15/#416482]a run down of all the changes and the usage[/url]. I've added the changed [url=http://codeigniter.com/wiki/d88426124937d7e6d32ce4cc7d8cd9f7/]file[/url] 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 [code]require_once APPPATH.'CodeIgniter'.EXT;[/code] 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.

Clone this wiki locally