0
votes

I'm using a common set of javascript across several Symfony modules. I'd like to output the current module url-key as a javascript variable, so the javascript can use it to construct urls for various AJAX calls. I can't find where to get it, though.

$sf_context->getModuleName();

returns the module name, but not how it appears in the URL. I get that I could parse the module name from window.location, but that not only seems a bit crude, but I will soon have a case where I construct a url to a different module than the one that generated the current page.

How does one get the URL-key for the current (or given) module? Surely that mapping exists for the front controller.

2

2 Answers

1
votes

To get the exact value as it was written in the url you can use custom routings. In symfony you can create a routing rule with the module name as a variable in the url. This variable named whatever you want will then be available in your action.

For example (app/*yourAppName*/config/routing.yml):

the_name_of_your_route:
  url:    thisIsAUrl/:variableYouWantWithWhateverCase
  param:  { module: yourModuleName, action: yourActionName }

Your url: param can even have regEx and wildcards in them if you like.

So now when you are in your executeYourActionName function a variable named variableYouWantWithWhateverCase will be set in the request exactly how it was typed in the url.

Hope this helps.

0
votes

You can get the url-value of the current module with:

$sf_params->get('module');  // in templates

and

$request->getParameter('module');       // either this
$this->getRequestParameter('module');   // or this in the actions

and for constucting URLs to other modules I can use url_for(), though I couldn't get the module url-value alone via this method (it generates an entire url).