I'm having trouble with my component and Joomla's SEF links. I'm trying to use JRequest::getVar
to get the variables from the original URL (specified with JRoute::_
)
My router.php file looks like this:
function PortfolioBuildRoute(&$query)
{
$segments = array();
if (isset($query['category'])) {
$segments[] = $query['category'];
unset($query['category']);
}
if (isset($query['subcategory'])) {
$segments[] = $query['subcategory'];
unset($query['subcategory']);
}
return $segments;
}
function PortfolioParseRoute($segments)
{
$vars = array();
$count = count($segments);
if ($count) {
$count--;
$segment = array_shift($segments);
if (is_numeric($segment)) {
$vars['subcategory'] = $segment;
} else {
$vars['category'] = $segment;
}
}
if ($count) {
$count--;
$segment = array_shift($segments) ;
if (is_numeric($segment)) {
$vars['subcategory'] = $segment;
}
}
return $vars;
}
The URL I'm encoding originally looks like:
index.php?option=com_portfolio&category=x&subcategory=y
and JRoute::_
turns it into /portfolio/x/y
. What I need now is some way of getting the variables x and y after the url is encoded?
----EDIT----
Ok so I figured it out - I changed the ParseRoute part of the router.php file to:
function PortfolioParseRoute($segments)
{
$vars = array();
$vars['category'] = str_replace(":", "-", $segments[0]);
$vars['subcategory'] = str_replace(":", "-", $segments[1]);
return $vars;
}
I feel I've got a slightly better understanding of the router.php file now. It turns out JRoute converts hyphens in your url to colons! Don't quite know why it picks on the poor hyphens, big JRoute bully. I could use underscores in the URL and it would function fine but hyphens are better SEO than underscores.
I used str_replace on each of the segments in ParseRoute to sort this out.
I'm not sure if this is the correct and standards way to go about this but I'm a Joomla and PHP noob so it will have to do until I'm advised otherwise.
At least it works!
:)