(Symfony 4)FOS Js路由捆绑包-路由-------不存在。

时间:2018-12-21 21:24:24

标签: javascript routes symfony4

似乎Fos Js Routing捆绑包没有拾取yml路由文件。到目前为止,这是我所做的:

设置:

$./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

在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>

在单独的.js模块文件中:

const routes = require('../../public/js/fos_js_routes.json');
import Routing from '../../vendor/friendsofsymfony/jsrouting-bundle/Resources/public/js/router.min.js';

在config \ routes.yaml中:

add_post_comment:
    path:   /comment/add/post
    controller: App\Controller\CreatorDashboard\CommentController::addPostComment
    requirements:
     _method:  POST

在我的javascript .js文件中:

$(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);

在JavaScript控制台中,出现错误:

Error: The route "add_post_comment" does not exist.

也许看不到yaml文件?我缺少步骤了吗?

我还重新启动了Nginx。

1 个答案:

答案 0 :(得分:0)

您不会像这样暴露路线:

# app/config/routing.yml
my_route_to_expose:
    pattern: /foo/{id}/bar
    defaults: { _controller: AppBundle:Default:index }
    options:
        expose: true