我面临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 Assets :install --symlink,但没有结果。
这是 Controller 对应的代码(实际代码有点长,我认为它不相关,如果您这样认为,我还是可以这样写:
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 app/console router:debug的结果(我只留下了相关信息+我留下了 undefined variable 通知,以防万一,这是我添加此库后收到的通知,但该库仍在运行,我认为问题可能不存在来自这里:
[路由器]当前路线
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 Notice,但它在我做的每条命令中都会发生):
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错误:
最佳答案
我也有这个问题。在路由配置上设置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
});
根据需要进行更改。希望这能解决您的问题。