chris
chris

Reputation: 36937

Codeigniter Routes, unexpected behavior and don't know why

So, I don't know how many times I have done similar with code igniter in the past across a handful of sites. However, just getting myself a fresh copy, and starting a rebuild on a new site Im already running into a twisted little issue that I can't figure out, I don't know what I am missing in this equation..

So this is my Routes currently:

$route['default_controller'] = "home";
$route['404_override'] = 'home/unknown';

$route['post/(:any)/(:any)'] = 'post/$1/$2';
//$route['post/(:any)'] = 'post/$1';
//$route['post'] = 'post';

$route['host-hotel'] = 'host_hotel';
$route['floor-plan'] = 'floor_plan';
$route['wine-facts'] = 'wine_facts';
$route['exhibitor-application'] = 'exhibitor_application';

$route['photo-gallery'] = 'photo_gallery';
$route['video-gallery'] = 'video_gallery';

Now the problem is specifically with this guy.

$route['post/(:any)/(:any)'] = 'post/$1/$2';

I've even tried naming the segment article instead of post, thinking maybe its a protected name of sorts in CI. If you notice above this line I have also tried adding varations of the URL so it could handle either just the first segment or one or two more there after. Grant it they are commented out in this example, but didn't work.

If I go to the domain.com/post the behavior is as expected. Anything there after is where the issue starts. If I did anything number or letter or combo there of..

ie: domain.com/post/s2hj or domain.com/post/s2hj/avd the page starts acting like the 404 behavior, page not found which to me makes no sense, as I said Ive done routes like this in the past. And to me this route looks proper? So anyone have any ideas/suggests what to look for?

Upvotes: 1

Views: 142

Answers (2)

Robin Castlin
Robin Castlin

Reputation: 10996

Another way to try this is through Good ol' regexp:

$route['post/([^/]+)/([^/]+)'] = 'post/$1/$2';

If also you need to be able to handle a total of 2 uri segments, consider:

$route['post/([^/]+)(?:/([^/]+))'] = 'post/$1/$2';

REGEXP explained:

() simply captures and handles the strings inside. Also gives us $1, $2 etc at the other end.

(?:) is the same as above, but doesn't capture $1

[] is the allowed combination of things inside. [0-9] will look for numbers only.

[^] is the reversed logic. everything exepct the given signs.

[^/] is everything but /, thus giving us desired result.

Upvotes: 1

Narf
Narf

Reputation: 14752

:any matches literally any character, including slashes. This will be changed in CodeIgniter 3.0, but for the time being you can use ([^/]) to catch a single segment.

Upvotes: 2

Related Questions