FOSJSRoutingBundle : “Route xxx 不存在”

2022-08-30 19:31:05

我遇到了一个非常奇怪的问题 FOSJSRoutingBundle:

首先,这是我的配置:我正在使用Symfony 2.0.23和JQuery,在Windows 7 64位上使用WAMP(Apache 2.4.2和PHP 5.4.3)。我已经完成了FOSJSRoutingBundle的github的所有设置,并公开了我的路由(几乎所有我可以通过谷歌搜索找到的相关问题(在FOSJSRoutingBundle的github上,这里和不同的论坛上)是因为人们没有暴露他们的路由,但我尝试了php app/console fos:js-routing:debug,我确实看到了我的路线)。js 被添加到布局中(布局的代码位于末尾)。

尝试在js中为路由生成url,一开始我想生成两个不同的路由,但为了测试,我在下面创建了js代码:

    //Code inside this function is working
    $("select").change(function () {
      param=this.options[this.selectedIndex].value;
      test1=Routing.generate('myBundle_step3', { myParam: param });
      alert(test1);
      window.location=Routing.generate('myBundle_step2');
    });
    //Code inside this one is also working
    $('input[type="checkbox"]').change(function() {
      test=Routing.generate('myBundle_step2');
    }).change();
    //This is not working
    test=Routing.generate('myBundle_step2');
    alert(test);

使用此代码,我收到javaScript错误“路由myBundle_step2不存在”。虽然第一部分仍然有效(警报给了我创建的链接,重定向进展顺利)。如果我删除第二个函数,我就不会再收到Javascript错误了。

如果在第二个函数中,我将 step2 替换为 step3,则错误将变为“路由myBundle_step3不存在”。我试图清除缓存并再次运行php app/console资产:install --symlink,但没有结果。

这是控制器对应的代码(实际代码有点长,我认为它不相关,如果你这么认为,我无论如何都可以把它放进去:

    namespace my\Bundle\Controller;
    class IndividuController extends Controller
    {
      public function step2Action() {
        Some code
      }
      public function step3Action($myParam) {
        Some code
      }
    }

路由.yml 配置文件相对于捆绑包:

    myBundle_step2:
      pattern: /step/2
      defaults: {_controller: myBundle:Individu:step2}
      options:
        expose: true
    myBundle_step3:
      pattern: /step/3/{myParam}
      defaults: {_controller: myBundle:Individu:step3}
      options:
        expose: true

The app/config/routing.yml file :

    fos_js_routing:
      resource: "@FOSJsRoutingBundle/Resources/config/routing/routing.xml"

    myBundle:
      resource: "@myBundle/Resources/config/routing.yml"
      prefix:   /

布局的相关信息:

     <!-- jQuery via Google + local fallback, see h5bp.com -->
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js">    </script>
        <script>window.jQuery || document.write('<script src="js/jquery-1.7.1.min.js"><\/script>')</script>
           {% javascripts 'bootstrap/js/bootstrap.js'
                        'bundles/fosjsrouting/js/router.js'
                        'bundles/crrisuaps/js/suaps.js' %}
               <script type="text/javascript" src="{{ asset_url }}"></script>
           {% endjavascripts %}
        <script src="{{ path('fos_js_routing_js', {"callback": "fos.Router.setData"}) }}"></script>
       </body>
    </html>

php app/console router:debug的结果(我只留下了相关信息+我留下了未定义的变量通知以防万一,这是我添加此库以来收到的通知,该库仍然有效,我认为问题可能来自这里:

C:\wamp\www\suapsRepo\suaps>php app/console router:debug

注意:未定义变量:c:\wamp\www\suapsRepo\suaps\vendor\html2 pdf_class\tcpdfConfig.php第 80 行

调用堆栈:0.0070 231536 1。{主}() C:\wamp\www\suapsRepo\suaps\app\console:00.0209 685656 2.require_once('C:\wamp\www\suapsRepo\suaps\app\bootstr ap.php.cache') C:\wamp\www\suapsRepo\suaps\app\console:10 0.0212 701752 3.require_once('C:\wamp\www\suapsRepo\suaps\app\autoloa d.php') C:\wamp\www\suapsRepo\suaps\app\bootstrap.php.cache:3 0.1335 2998152 4.require_once('C:\wamp\www\suapsRepo\suaps\vendor\html 2pdf\html2pdf.class.php') C:\wamp\www\suapsRepo\suaps\app\autoload.php:51 0.1379 3361792 5.require_once('C:\wamp\www\suapsRepo\suaps\vendor\html 2pdf_class\myPdf.class.php') C:\wamp\www\suapsRepo\suaps\vendor\html2pdf\html2p df.class.php:19 0.1385 3393792 6.require_once('C:\wamp\www\suapsRepo\suaps\vendor\html 2pdf_class\tcpdfConfig.php') C:\wamp\www\suapsRepo\suaps\vendor\html2pdf_class \myPdf.class.php:12

[路由器]当前路线

    Name                                            Method Pattern
    _assetic_55f0319                                ANY    /css/55f0319.css
    _assetic_55f0319_0                              ANY    /css/55f0319_bootstrap_1.
    css
    _assetic_55f0319_1                              ANY    /css/55f0319_bootstrap-re
    sponsive_2.css
    _assetic_55f0319_2                              ANY    /css/55f0319_style_3.css
    _assetic_3608a04                                ANY    /js/3608a04.js
    _assetic_3608a04_0                              ANY    /js/3608a04_bootstrap_1.j
    s
    _assetic_3608a04_1                              ANY    /js/3608a04_router_2.js
    _assetic_3608a04_2                              ANY    /js/3608a04_suaps_3.js

    fos_js_routing_js                               ANY    /js/routing.{_format}
    myBundle_homepage                        ANY    /

    myBundle_inscription_etape1              ANY    /inscription/etape/1
    myBundle_inscription_etape2              ANY    /inscription/etape/2
    myBundle_inscription_etape3              ANY    /inscription/etape/3/{dis
    ciplineSelection}

php app/console fos:js-routing:debug 的结果(我删除了PHP通知,但它发生在我做的每个命令上顺便说一句):

    C:\wamp\www\suapsRepo\suaps>php app/console fos:js-routing:debug
    [router] Current routes
    Name                               Method Pattern
    crrisuapsBundle_inscription_etape2 ANY    /inscription/etape/2
    crrisuapsBundle_inscription_etape3 ANY    /inscription/etape/3/{disciplineSelect
    ion}

编辑:另外,不知道它是否相关,但是当我尝试“ php app/console fos:js-routing:debug myBundle_step2”时,我得到以下php错误:

[错误异常]警告:Symfony\Bundle\FrameworkBundle\Command\RouterDebugCommand:outputRoute() 缺少参数 3,在 C:\symfonyDirectory\vendor\bundles\FOS\JsRoutingBundle\Command\RouterDebugExposedCommand.php在第 62 行上,并在 C:\serverDirectory\vendor\symfony\src\Symfony\Bundle\FrameworkBundle\Command\RouterDebugCommand.php第 98 行中定义


答案 1

我也有这个问题。在路由配置上设置参数为我解决了这个问题。options.expose

routing.yml

cart_edit:
    pattern:  /Cart/edit
    defaults: { _controller: CartCartBundle:Default:cart_edit }
    options:
        expose: true

我的.js

var url = Routing.generate('cart_edit');
$.post(url, function(data) {
      //do somthing
});

根据需要进行更改。希望这将解决您的问题。


答案 2

我遇到了同样的问题,问题出在我的配置文件中。请尝试检查以下内容:

# app/config/config.yml
fos_js_routing:
    routes_to_expose: [ myBundle_step2, myBundle_step3, ... ]