(Symfony 4) FOS Js Routing Bundle - The route ----— does not exist.

北城以北 提交于 2020-01-05 04:58:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!