Question

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!

Was it helpful?

Solution

Assuming you do not want to use the index (i.e. http://www.yourdomain.com/category) action of your Categories controller, you can use routes.

$route['category/(:any)'] = 'category/view/$1';

Then you simply need a View action within your Category controller to receive the Category name, i.e. PHP.

http://www.yourdomain.com/category/PHP

function View($Tag)
{
    var_dump($Tag);
}

Should you still want to access your index action within your controller, you can still access it via http://www.yourdomain.com/category/index

OTHER TIPS

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.

$default_controller = "Home";

$language_alias = array('gr','fr');

$controller_exceptions = array('signup');

$route['default_controller'] = $default_controller;

$route["^(".implode('|', $language_alias).")/(".implode('|', $controller_exceptions).")(.*)"] = '$2';

$route["^(".implode('|', $language_alias).")?/(.*)"] = $default_controller.'/$2';

$route["^((?!\b".implode('\b|\b', $controller_exceptions)."\b).*)$"] = $default_controller.'/$1';

foreach($language_alias as $language)

$route[$language] = $default_controller.'/index';
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top