4

So I am using a Codeigniter 2.1 Internationalization i18n Library and I need to adapt my routes to use the language parameters in my short urls where I remove the controller's name:

$route['default_controller'] = "home";

#$route['^(en|es|ro)/(.+)$'] = "$2";
#$route['^(en|es|ro)$'] = $route['default_controller'];

$route['results$']      = "fetch/results";
$route['video/(:any)']  = "fetch/video/$1";
$route['tag/(:any)']    = "fetch/tag/$1";

$route['404_override'] = '';

So I need to make this work http://localhost/app/en/results?query=t-shirts instead of http://localhost/app/en/fetch/results?query=t-shirts

How can I accomplish that?

Edit:

$route['^(en|es|ro)/results$'] = "fetch/results";
$route['^(en|es|ro)/video/(:any)'] = "fetch/video/$1";
$route['^(en|es|ro)/video/(.+)$'] = $route['^(en|es|ro)/(.+)$'] = "$2";"fetch/video/$2";

None of the above works.

If I do http://localhost/app/en/fetch/video/asfasf works perfectly

This doesn't http://localhost/app/en/video/asfasf

The error I get is a 404:

404 Page Not Found

The page you requested was not found.

Found the problem

So, for some reason having this set : $route['^(en|es|ro)/(.+)$'] = "$2"; before what I was trying to do was causing the problem.

tereško
  • 58,060
  • 25
  • 98
  • 150
Alex
  • 7,538
  • 23
  • 84
  • 152

2 Answers2

2

This kind of defeats the purpose of controllers, but if you know that anything with "results" as the second segment should use the "fetch" controller, you could do this:

$route['^(en|es|ro)/results$'] = "fetch/results";

EDIT:

It's because your (:any) line is catching before the right one. Also, a "." (dot) character will match a forward slash too, which probably isn't what you want.

$route['^(en|es|ro)/results$']       = "fetch/results";
$route['^(en|es|ro)/video/([^/]+)$'] = "fetch/video/$2";
$route['^(en|es|ro)/video/(:any)']   = "fetch/video/$1";
landons
  • 9,502
  • 3
  • 33
  • 46
2

For anybody that uses that library and wants to set custom routes, in order for them to work you'll have to do it in this order:

  1. custom routes
  2. The routes added by the library

example:

$route['default_controller'] = "home";

//First
$route['^(en|es|ro)/video/(.+)$'] = "fetch/video/$2";
$route['^(en|es|ro)/results$']     = "fetch/results$2";
//Second
$route['^(en|es|ro)/(.+)$']        = "$2";
$route['^(en|es|ro)$'] = $route['default_controller'];


$route['404_override']  = '';
Alex
  • 7,538
  • 23
  • 84
  • 152