FOSJSRoutingBundle:“路线xxx不存在”

时间:2013-04-16 07:12:23

标签: php javascript symfony

我正面临着一个非常奇怪的问题: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错误。 如果在第二个函数中我将步骤2替换为步骤3,则错误变为“路径myBundle_step3不存在”。 我试图清除缓存并运行php app / console asset:再次安装--symlink,但没有结果。

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

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

相对于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

app / config / routing.yml文件:

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应用程序/控制台路由器的结果: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

[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}

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,在第62行的C:\ symfonyDirectory \ vendor \ bundles \ FOS \ JsRoutingBundle \ Command \ RouterDebugExposedCommand.php中调用,并在C:\ serverDirectory中定义\ vendor \ symfony \ src \ Symfony \ Bundle \ FrameworkBundle \ Command \ RouterDebugCommand.php第98行

3 个答案:

答案 0 :(得分:54)

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

<强>的routing.yml

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

<强> my.js

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

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

答案 1 :(得分:6)

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

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

答案 2 :(得分:2)

为了完整性:还可以使用注释(options={"expose"=true}),如documentation中所述:

// src/AppBundle/Controller/DefaultController.php

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

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

/**
 * 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
{
相关问题