Ellana
Ellana

Reputation: 43

FOSJSRoutingBundle - Routing.generate doesn't do anything

I'm facing a bit of a trouble with the FOSJsRoutingBundle in Symfony4 when trying to load a new template from my Javascript file.

I followed the instructions over here.

The route I want to call is as follow :

 /**
  * @Route("/", name="homepage", options={"expose" = true})
  */
  public function homepage()
  {
     return $this->render('home.html.twig');
  }

When

 Routing.generate('homepage');
 alert("Finished!");

in my Javascript file gets called, the alert gets displayed and nothing else happens. I should probably add that I'm obviously not on the homepage when this gets called.

However, if I purposefully misspell the name of the Route or if I call a route that has not been exposed via

options={"expose" = true}

I do get an error :

Error: The route "misspelled_route" does not exist. 2 router.js:220:27

and no alert gets displayed, which leads me to believe that I followed the instructions on the Symfony website correctly (?).

Am I missing something obvious or poorly using the bundle ?

Thank you very much !

Upvotes: 1

Views: 1688

Answers (1)

Ellana
Ellana

Reputation: 43

I'm not entirely sure if I'm supposed to answer my own question like this, but if anybody has the same problem when following the Symfony website's instruction on FOSJsRoutingBundle :

Routing.generate returns a String. If you're like me and want the page to redirect, you can use

window.location.href = Routing.generate('your_route_name');

Thanks to pbenard for this !

Upvotes: 1

Related Questions