MisterJ MisterJ - 2 months ago 13
PHP Question

FOSJSRoutingBundle : "Route xxx does not exist"

I'm facing a really weird problem with 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).

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 :

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

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 :

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: /


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 :

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}


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

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 :

[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

Answer

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

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
});

Change as needed. Hope this will solve your problem.