Mike Rifgin
Mike Rifgin

Reputation: 10745

Custom route in Code Igniter being overwritten

I have these routes:

$route['shop/(:any)/(:any)'] = 'product/category_listing/$1/$2';
$route['shop/(:any)/(:any)/(:any)'] = 'product/product_listing/$1/$2/$3';

When I call this url:

http://mysite.com/shop/mens/trainers/a-product

the product_listing method should be called but instead the first method (category_listing) gets called and product_listing is never invoked.

How can I make this work as required?

Upvotes: 1

Views: 83

Answers (1)

Aleksej Komarov
Aleksej Komarov

Reputation: 194

Order of array elements matters!

Keyword (:any) matches everything, even slashes, so in your example CodeIgniter finds the first matching route and doesn't look any further.

So, if we do like this:

$route['shop/(:any)/(:any)/(:any)'] = 'product/product_listing/$1/$2/$3';
$route['shop/(:any)/(:any)'] = 'product/category_listing/$1/$2';

...then product listing is matched first, then everything else.

Even more, you can use regular expressions (e.g. ([a-z0-9]+)) to create rules you need.

Upvotes: 3

Related Questions