0

Here my route

$route['posts/(:any)'] = 'posts/get_show/$1';
$route['posts/(:any)/dosomething'] ='posts/get_dosomething/$1';

Controller

public function get_show($id)
public function get_dosomething($id)

With the link posts/1/dosomething, route always points to the action get_show, it never goes to get_dosomething

Any idea how to fix it ? Thanks

Hoan Dang
  • 2,222
  • 5
  • 27
  • 36

2 Answers2

2

you must first route like this

$route['posts/(:any)/dosomething'] ='posts/get_dosomething/$1';
$route['posts/(:any)'] = 'posts/get_show/$1';
  • Oh dear, that seems silly. Could you explain why it works that way ? – Hoan Dang Feb 02 '13 at 09:01
  • Becaue this `any` is greedy. Unlike other frameworks, CI treats it as optional for any number of parameter. – itachi Feb 02 '13 at 09:03
  • 1
    it's like a book .. also don't jump to the end.. you said in your routes, that posts/(:any) is calling get_show()... and now you calling posts/(:any)/something... therefore it must be in this order – Johnny X. Lemonade Feb 02 '13 at 09:05
  • That's a bit wrong approach from CI. :any is one segment. If i say `post/foo` then it is ok. But if it is `post/foo/bar` then it shouldn't pass through that filter because there is an extra segment. – itachi Feb 02 '13 at 09:22
  • To explain why it works CI routes by the first rule that matches (and it isn't just CI that does this a lot of systems do). So since (:any) is a wildcard it will match anything in the first segment after posts and stops. You have to go from the most specific to the widest range when routing, keep that rule in mind and you'll be fine. – Rick Calder Feb 02 '13 at 16:49
0

because posts/1/dosomething is always fallen under $route['posts/(:any)'] . change one of your routes condition

Ripa Saha
  • 2,532
  • 6
  • 27
  • 51