I'm trying use FOSJsRoutingBundle to generate urls from symfony routes.
I follow the doc.
I include the files into my base.html.twig:
{% block scripting %}
...
<script type="text/javascript" src="{{ asset('bundles/fosjsrouting/js/router.js') }}"></script>
<script src="{{ path('fos_js_routing_js', {"callback": "fos.Router.setData"}) }}"></script>
...
{% endblock %}
And don't get inclusion error. But the try to generate an url fails:
console.log(Routing.generate('usuarios_crear'));
GET http://appsmanantiales.local/web/app_dev.php/js/routing?callback=fos.Router.setData 500 (Internal Server Error) listar:131 Uncaught Error: The route "usuarios_crear" does not exist. router.js:9
In my routing.yml I have:
usuarios_crear:
pattern: /AbmUsuarios/crear
defaults: { _controller: AbmBundle:Usuario:crear }
options:
expose: true
And the route is in php app/console fos:js-routing:debug list:
usuarios_crear ANY ANY ANY /AbmUsuarios/crear
Any ideas?
Your code is great.
I suggest that you open an issue here, this is very likely to be a bug as you're not the only one to have that issue.
It's probably easier than you think.
Visit the page generated here: {{ path('fos_js_routing_js', {"callback": "fos.Router.setData"}) }} it should have json-encoded list of your exposed routes. If your route is there you're good.
How check when you execute console.log(Routing.generate('usuarios_crear'));
The document should be ready before you try that. So make sure it is (i.e. jQuery's $('document').ready()).
Reading the Github issues, found this where the author suggest not use the dev-master version. I do a downgrade to "@stable", and works (for now). The composer.json line may be:
"friendsofsymfony/jsrouting-bundle": "@stable"
For more detail: The full install guide.
Don't require dev-master in a composer.json file, never ever, ever! It is a bad practice.
Rely on stable releases please: https://github.com/FriendsOfSymfony/FOSJsRoutingBundle/releases
I just had the same issue, and the solution was include the ^/js/* route to access anonymously in your security configuration.
I hope it helps!
Try to use this code
Suppose you have the an onclick function that receives an id when clicked so your js code will something like this
function show(id)
{
var url = '{{ path('usuarios_crear',{ id: 'id' }, {"callback": "fos.Router.setData"}) }}';
url = url.replace("id", id);
window.location.href = url;
}
来源:https://stackoverflow.com/questions/21058326/fosjsroutingbundle-dont-recognize-route