0
votes

Hey. I was going to make a "current" class for my menu component, that creates the menus. To find out the current class, it was suggested to use $this>getContext()->getRouting()->getCurrentRouteName();. To do this, I had to set up routes for all the menu-elements that was going to exist.

Lets say I have two elements so far, one for the frontpage, one for the customer. Whenever I am browsing the frontpage, matter what action and parameters, I would like the above code to return "frontpage". Same goes for customer.

My rule for customer now, is the following: routing.yml

customer:
  param: { module: customer }

I thought that setting up the url was not important, I have already specified the module to be customer. The menu element should have "current" class no matter the action used.

How I link:

if ($current == $name){
    echo link_to($name, $link, array("class" => "selected"));
}else {
    echo link_to($name, $link);
}

Links ($link) that now go to "customer/some_action" does not work, they show up as just "/". So I thought to use url_for around the $link, that didn't work either, same result.

There must be something wrong with my routing rules... Because when I enter lets say /customer/new manually, I can see the page. If I from the template write out $this>getContext()->getRouting()->getCurrentRouteName();, it says default. So my rule definitely does not work at all. I've tried other rules too, but haven't got anyone working. The trick is to make one rule that works for anything under the module customer..

And yes, I clear my cache after changing each of my rules.

Thanks a lot.

EDIT: I made the routing rules work, but not for all actions under the customer module. I have to make rules for each action. But, the links are still broken, they show "/".

My new rules:

customer_index:
  url:   /:module
  param: { module: customer, action: index }

customer_new:
  url:   /:module/:action
  param: { module: customer, action: new }

None of these links works: echo link_to("Customers", "@customer_index", array("module" => "customer"));

echo link_to("New customer", "@customer_new", array("module" => "customer"));

1

1 Answers

1
votes

Yep, this is how I've done it a few times as well. You need separate routing rules to use getCurrentRouteName().

The problem you have with the links is that you cannot pass the "module" parameter in this way. It's not a valid parameter for the Symfony link_to() helper. Instead, you can pass it this way:

link_to('Customers', '@customer_index?url_variable=something');

customer_index:
  url:   /:url_variable
  param: { module: customer, action: index, url_variable: some_default_value }

The default value is optional, but the link_to helper will throw an error if you don't pass the variable and the route doesn't have a default value for it.

Also, just pointing this out as you've got it twice wrong in your code above:

$this>getContext()->getRouting()->getCurrentRouteName();
...should be:
$this->getContext()->getRouting()->getCurrentRouteName();