CodeIgniter Forums

Full Version: Priority of routes using 'generic' add method
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,
I had an issue Confused that that a route defined with 'add' was given lower priority than a route defined with 'get' method.

Route definition:

$routes->get('news/create','News::create');
$routes->get('news/test','News::test');
$routes->get('news(/Confusedegment)?','News::view$1');


Order of route resolved in  \Codeigniter\Router\Collection\getRoute()

array (
  'news/create' => '\\App\\Controllers\\News::create',
  'news/test' => '\\App\\Controllers\\News::test',
  'news(/[^/]+)?' => '\\App\\Controllers\\News::view$1',
)



Route definition:

$routes->add('news/create','News::create');
$routes->get('news/test','News::test');
$routes->get('news(/Confusedegment)?','News::view$1');



Order of route resolved in  \Codeigniter\Router\Collection\getRoute() 

array (
  'news/test' => '\\App\\Controllers\\News::test',
  'news(/[^/]+)?' => '\\App\\Controllers\\News::view$1',
  'news/create' => '\\App\\Controllers\\News::create',
)

Reviewing the code, this appears to be by design.

if (isset($this->routes[$verb]))
{
  // Keep current verb's routes at the beginning so they're matched
  // before any of the generic, "add" routes.
  if (isset($this->routes['*']))
  {
     $extraRules = array_diff_key($this->routes['*'], $this->routes[$verb]);
     $collection = array_merge($this->routes[$verb], $extraRules);
  }
  foreach ($collection as $r)
  {
     $key          = key($r['route']);
     $routes[$key] = $r['route'][$key];
  }
}



Although I don't necessarily agree with the rational behind this design decision, I think it would be create to document this in the user guide.

Not sure how to contribute to this, but willing to make pull request for the doco if someone can point me in he right direction on how to do this.

Thanks to anyone who can assist on this.