Reputation: 7688
I am trying to set a custom route for friendly url when loading a tag, by that I mean that I want to convert this: http://test.com/en/results?search_query=data&search=tag
to this http://test.com/en/tag/data
Tried to do so by setting this in my routes.php:
$route['^(en|es|ro)/tag/(:any)'] = "fetch/results?search_query=$1&search=tag";
But no success, am I doing it wrong?
My routes.php:
$route['default_controller'] = "home";
// URI like '/en/about' -> use controller 'about'
$route['^(en|es|ro)/video/(.+)$'] = "video/id/$2";
$route['^(en|es|ro)/results$'] = "fetch/results$2";
$route['(en|es|ro)/tag/(:any)'] = "fetch/results?search_query=$2&search=tag";
$route['^(en|es|ro)/(.+)$'] = "$2";
// '/en', '/de', '/fr' and '/nl' URIs -> use default controller
$route['^(en|es|ro)$'] = $route['default_controller'];
$route['404_override'] = '';
Upvotes: 1
Views: 1950
Reputation: 102874
I'm fairly sure the ^
is not necessary. Try without it:
$route['(en|es|ro)/tag/(:any)'] = "fetch/results?search_query=$1&search=tag";
In that context, ^
will be treated as a literal string. If you wanted top use a regex, I believe you'd have to wrap the regex itself in parentheses, like so:
$route['(^(en|es|ro))/tag/(:any)'] = "fetch/results?search_query=$1&search=tag";
I'm not 100% certain on that, but if I'm correct both should work. However, routes always assume the first part of the pattern is what the string should start with, so the ^
shouldn't be necessary anyways.
One more thing: You aren't using the second match, but the first. I believe you want to replace $1
with $2
in your route.
$route['(en|es|ro)/tag/(:any)'] = "fetch/results?search_query=$2&search=tag";
// ^^^1^^^^ ^^^2^^ ^^
EDIT: Upon further investigation and testing, it seems that Codeigniter cannot properly handle routes that use a query string like ?key=value
. If you use a slash before the ?
question mark, you can at least get it to route to the right controller and method:
$route['(en|es|ro)/tag/(:any)'] = "fetch/results/?search_query=$2&search=tag";
A url like this:
http://example.com/es/tag/Codeigniter's Router
...will be routed to the results
method with this string as an argument, but $_GET
will be empty:
?search_query=Codeigniter%27s%20Router&search=tag
So, you could pick apart the argument with parse_str()
:
function results($request)
{
$request = ltrim($request, '?');
parse_str($request, $get);
// echo $get['search_query']
}
You could always use .htaccess
of course, but I understand the desire to keep all the routing logic in one place. Sorry if this isn't the answer you hoped for, but I'm afraid that without writing a custom router it's not possible to do this the obvious way in Codeigniter. My suggestion would probably be to find another way to handle your routing and the way you're accessing data from the URL.
Upvotes: 3
Reputation: 7780
You may need to escape the forwardslashes around tag. So it should look like this:
$route['^(en|es|ro)\/tag\/(:any)']
Upvotes: 1
Reputation: 1744
I have something similar, and it looks like
$route['(:any)/something/(:any)'] = "real/$2/path/$1";
Upvotes: 0