rsanchez / resource_router

Resource Router for ExpressionEngine
MIT License
48 stars 12 forks source link

Way to get category ID within callback #16

Closed bryanburgers closed 10 years ago

bryanburgers commented 10 years ago

Is there any way to get the category ID within a callback after a valid Category URL Title is found? I realize I can use {route_X_cat_id}. But I'd like to set it to my own variable.

$env_config['resource_router'] = array(
  'news/:any/:pagination' => function($router, $any, $pagination) {
    if ($any->isValidCategoryUrlTitle()) {
      $router->setGlobal('my_category_id', /* HERE */);
      $router->setTemplate('home/news-category');
    }
    //...
  }
)

While {route_X_cat_id} works, I don't want to, within my template, depend on the fact that the :any is the first wildcard in my routes. If I change that to :page:3/:any/:pagination in the future, then I need to go into my template and change {route_1_cat_id} to {route_2_cat_id}.

rsanchez commented 10 years ago

You can use getMeta:

if ($any->isValidCategoryUrlTitle()) {
  $cat_id = $any->getMeta('cat_id');
bryanburgers commented 10 years ago

Ah, good deal! Any idea when getMeta will show up in an official release? For ease of maintenance among my team, we try to only use released versions of add-ons.

I downloaded 1.0.5 today and I get a 'undefined method getMeta' error.

rsanchez commented 10 years ago

Tagged v1.0.6 today, which has the getMeta method.

bryanburgers commented 10 years ago

Thanks!