问题
It seems like the routing yml file isn't being picked up by the Fos Js Routing bundle. Here's what I have done so far:
Setup:
$./composer.phar require friendsofsymfony/jsrouting-bundle
$bin/console assets:install --symlink public
$bin/console fos:js-routing:dump --format=json --target=public/js /fos_js_routes.json
In base.html.twig:
<script src="{{ asset('bundles/fosjsrouting/js/router.min.js') }}"></script>
<script src="{{ path('fos_js_routing_js', { callback: 'fos.Router.setData' }) }}"></script>
In a separate .js module file:
const routes = require('../../public/js/fos_js_routes.json');
import Routing from '../../vendor/friendsofsymfony/jsrouting-bundle/Resources/public/js/router.min.js';
In config\routes.yaml:
add_post_comment:
path: /comment/add/post
controller: App\Controller\CreatorDashboard\CommentController::addPostComment
requirements:
_method: POST
In my javascript .js file:
$(document).ready(function() {
$(".post-comment").keyup(function(e) {
var code = e.which;
if(code==13)
{
var commentText = $(this).val();
var postId = $(this).data("postid");
var userId= $(this).data("userid");
console.log("userId is " + userId + " and postId is " + postId + " and commentText is " + commentText);
var theRoute = Routing.generate('add_post_comment'); // have also tried Routing.generate('/comment/add/post');
alert("the route is " + theRoute);
In the JavaScript console I get the error:
Error: The route "add_post_comment" does not exist.
Maybe it doesn't see the yaml file? Is there a step I'm missing?
I have also restarted nginx.
回答1:
You are not exposing your route like this:
# app/config/routing.yml
my_route_to_expose:
pattern: /foo/{id}/bar
defaults: { _controller: AppBundle:Default:index }
options:
expose: true
来源:https://stackoverflow.com/questions/53890929/symfony-4-fos-js-routing-bundle-the-route-does-not-exist