From Symfony 4, I need to generate dynamicals url path of my controller (controller who need, in the futur, some parameters) from javascript.
After some google searches I found and installed the jsrouting-bundle via a simple composer require friendsofsymfony/jsrouting-bundle (I executed the recipe)
I include theses lines in my twig file :
<script src="{{ asset('bundles/fosjsrouting/js/router.js') }}"></script>
<script src="{{ path('fos_js_routing_js', { callback: 'fos.Router.setData' }) }}"></script>
<script src="build/js/global.js"></script>
When I load my page, no javascript errors appeared.
So, I created this very simple symfony controller (for an AJAX call) :
/**
* @Route("/API/test", name="ajax_test")
*/
public function test(){
$test = [
'a' => 'foo',
'b' => 'faa',
];
return new JsonResponse($test);
}
And in my custom javascript script (global.js) , I tried this :
alert(Routing.generate('ajax_test'));
But when I load the page, I get this javascript error :
Error: The route "ajax_test" does not exist.
Why the route is not found ? Where is my mistake ?
Notes :
in the friendsofsymfony/jsrouting-bundle documentation , from the when I executed the command
bin/console assets:install --symlink webI get the errorThe target directory "web" does not exist.. So, I executed simplybin/console assets:install --symlinkIf I go to the "http://localhost/mywebsite/public/js/routing?callback=fos.Router.setData", I get
/**/fos.Router.setData({"base_url":"\/mywebsite\/public","routes":[],"prefix":"","host":"localhost","port":"","scheme":"http"});
I just add the option "expose" set to true in annotations of my controller like this :