Closed siebird closed 10 years ago
I'm curious too, to know if the products
routing could be done more efficiently with maybe a callback?
Got it working–the pages module was turned on. Turned it off and now they work.
Pages module / Structure URLs take precedence over Resource Router. This is by design. I'll add a note of it in the docs.
First of all, I think how you have it set up is perfectly acceptable. But you asked how you could do your products section in one callback:
$config['resource_router'] = array(
'products/:category_url_title/:all' => function($router) {
if ( ! $router->wildcard(1)->isValidCategoryUrlTitle()) {
return $router->set404();
}
if ($router->wildcard(2)->isEmpty()) {
$router->setGlobal('product_cat_url_title', $router->wildcard(1));
return $router->setTemplate('site/products_category');
}
$isValidCat = $router->wildcard(2)->isValidCategoryUrlTitle(array(
'parent_id' => $router->wildcard(1)->getMeta('cat_id'),
));
if ( ! $isValidCat) {
$router->setGlobal('product_url_title', $router->wildcard(2));
return $router->setTemplate('site/product');
}
if ( ! $router->wildcard(3)) {
$router->setGlobal('product_cat_url_title', $router->wildcard(2));
return $router->setTemplate('site/products_category');
}
$isValidCat = $router->wildcard(3)->isValidCategoryUrlTitle(array(
'parent_id' => $router->wildcard(2)->getMeta('cat_id'),
));
if ( ! $isValidCat) {
$router->setGlobal('product_url_title', $router->wildcard(3));
return $router->setTemplate('site/product');
}
if ( ! $router->wildcard(4)) {
$router->setGlobal('product_cat_url_title', $router->wildcard(3));
return $router->setTemplate('site/products_category');
}
$router->setGlobal('product_url_title', $router->wildcard(4));
return $router->setTemplate('site/product');
},
);
This example assumes you have the latest version, 1.0.6, installed.
Rob, thanks for the example! This definitely helps for someone that is not addon developer. In your opinion, which way is more efficient: callback or the 6 lines of routes?
My advice is to use whatever's going to be most readable to you (or the next dev) 6 months from now. I don't think there's much of a performance difference between the two. The callback does a bit more than the first approach, in that it actually can validate whether the cat in segment 1 is actually the parent of the cat in segment 2, etc.
I appreciate the help and explanations. Thanks man!
Hey Rob,
Having an issue getting simple routes to work properly. All routes work until RR gets to the
warehouse-specials
route, this route and the:any
route are getting set with thesite/index
or404
template. Do you have any ideas on why this happening? In global template prefs I have the native TR turned off, strict urls off, 404 page set to correct template.Screen shot of entries in pages channel:
http://d.pr/i/1acav
RR configs: