_remap or URI Routing in codeigniter

Shimsham84 picture Shimsham84 · Aug 20, 2012 · Viewed 9.4k times · Source

I am currently looking into the PHP Framework Codeigniter and understand the main concepts so far until the Controllers section about _remapping. I have a understanding how _remapping overwrites the behaviour of controller methods over the URI eg from www.example.com/about_me to www.example.com/about-me. What i would like to hear is peoples opinions on what to use- _remapping method or the URI Routing method? Im only asking this as when researching these methods and someone has been troubled on remapping functions, they have been directed to use URI Routing.

So..

1) What is the main common method to use and the pro's over the other one? 2) Is it best to use URI Routing for PHP5 CI version 2 onwards?

I would be grateful to hear your opinions!

Answer

Touki picture Touki · Aug 20, 2012

You should use _remap if you want to change the behaviour of default CI's routing.

For example, if you set a maintenance and want to block any specific controller from running, you can use a _remap() function loading your view, and which will NOT call any other method.

Another example is when you have multiple methods in your URI. Example:

site.com/category/PHP
site.com/category/Javascript
site.com/category/ActionScript

Your controller is category but methods are unlimited. There you can use a _remap method as called by Colin Williams here: http://codeigniter.com/forums/viewthread/135187/

 function _remap($method)
{
  $param_offset = 2;

  // Default to index
  if ( ! method_exists($this, $method))
  {
    // We need one more param
    $param_offset = 1;
    $method = 'index';
  }

  // Since all we get is $method, load up everything else in the URI
  $params = array_slice($this->uri->rsegment_array(), $param_offset);

  // Call the determined method with all params
  call_user_func_array(array($this, $method), $params);
}  

To sum up, if the current CI's routing suits to your project, don't use a _remap() method.