php FOSJSRoutingBundle:“路由xxx不存在”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16030900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:24:37  来源:igfitidea点击:

FOSJSRoutingBundle : "Route xxx does not exist"

phpjavascriptsymfony

提问by MisterJ

I'm facing a really weird problem with FOSJSRoutingBundle :

我正面临着 FOSJSRoutingBundle 一个非常奇怪的问题:

First of all, here is my configuration : I'm working on Symfony 2.0.23 and with JQuery, on Windows 7 64 bits with a WAMP (Apache 2.4.2 and PHP 5.4.3). I've done all the settings from the FOSJSRoutingBundle's github and haved exposed my routes ( almost all the related problems I could find by googling (on FOSJSRoutingBundle's github,here and on different forums) were because people have not exposed their routes, but I tried php app/console fos:js-routing:debug and I do see my routes). The js is added to the layout (code of the layout at the end).

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

Trying to generate url for routes in js, at the beginning I wanted to generate two different routes but for test I created the js code below :

尝试为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);

With this code, I get the javaScript error "The route myBundle_step2 does not exist". Though the first part still works (alert gives me the link created and the redirection goes well). If I remove the second function, I do not get the Javascript error anymore.

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

If in the second function I replace step2 by step3, the error becomes "The route myBundle_step3 does not exist". I tried to clear the cache and to run php app/console asset:install --symlink again, but no results.

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

Here is the controller corresponding code (the real code is a bit long and I don't think it's relevant, if you think so, I could put it anyways :

这是控制器对应的代码(真正的代码有点长,我认为它不相关,如果你这么认为,我可以把它反正:

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

The routing.yml config file relative to the bundle :

与 bundle 相关的 routing.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 :

app/config/routing.yml 文件:

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

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

The layout's relevant informations :

布局的相关信息:

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

Result of php app/console router:debug (I left only the relevant informations + I left the undefined variable notice just in case, it's a notice I got since I added this library, still the library works and I do not think the problem could come from here :

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

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

Notice: Undefined variable: kPathUrl in C:\wamp\www\suapsRepo\suaps\vendor\html2 pdf_class\tcpdfConfig.php on line 80

Call Stack: 0.0070 231536 1. {main}() C:\wamp\www\suapsRepo\suaps\app\console:0 0.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

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

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

调用堆栈:0.0070 231536 1. {main}() C:\wamp\www\suapsRepo\suaps\app\console:0 0.0209 685656 2. require_once('C:\wamp\www\suapsRepo\suaps\app\bootstr .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

[router] Current routes

[路由器] 当前路由

    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}

Result of php app/console fos:js-routing:debug (I removed the PHP Notice but it happens on every command I make btw) :

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}

Edit : Also, don't know if it's relevant but when I try 'php app/console fos:js-routing:debug myBundle_step2' I get the following php error :

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

[Error Exception] Warning: Missing argument 3 for Symfony\Bundle\FrameworkBundle\Command\RouterDebugCommand:outputRoute(), called in C:\symfonyDirectory\vendor\bundles\FOS\JsRoutingBundle\Command\RouterDebugExposedCommand.php on line 62 and defined in C:\serverDirectory\vendor\symfony\src\Symfony\Bundle\FrameworkBundle\Command\RouterDebugCommand.php line 98

[错误异常] 警告: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 行

回答by Azam Alvi

I also had this problem. Setting the options.exposeparameter on the routing configuration solved it for me.

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

routing.yml

路由.yml

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

my.js

我的.js

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

Change as needed. Hope this will solve your problem.

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

回答by Ehsan

I had the same issue and the problem was in my config file. Try checking for this:

我有同样的问题,问题出在我的配置文件中。尝试检查这个:

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

回答by Aerendir

Just for completeness: it is also possible to use annotations (options={"expose"=true}) as explained in the documentation:

只是为了完整性:也可以使用注释(options={"expose"=true}),如文档中所述:

// src/AppBundle/Controller/DefaultController.php

/**
 * @Route("/foo/{id}/bar", options={"expose"=true}, name="my_route_to_expose")
 */
public function indexAction($foo) {
    // ...
}

This also works for routes sets at controller level (example from one of my controllers):

这也适用于控制器级别的路由集(来自我的控制器之一的示例):

/**
 * Channel controller.
 *
 * @Route("account/{account}/todos", options={"expose"=true})
 * @ParamConverter("account", class="AppBundle:Account", options={
 *     "repository_method" = "findOneById",
 *     "mapping": {"account": "id"},
 *     "map_method_signature" = true
 * })
 */
class TodoListController extends Controller
{