FriendsOfSymfony / FOSJsRoutingBundle

A pretty nice way to expose your Symfony routing to client applications.
1.48k stars 261 forks source link

Prod route generated instead of Dev one... #326

Closed picks44 closed 6 years ago

picks44 commented 6 years ago

How come I cannot generate a http://myproject.local/app_dev.php/myroute with the bundle ? I tried several config and the route is always generated as http://myproject.local/myroute whereas I work in Dev mode...

I use Webpack Encore and dump the files using yarn run encore dev

Is there something I missed ?

stof commented 6 years ago

how are you providing your routes ? Are you dumping the route file, or are you loading them from the controller ?

picks44 commented 6 years ago
/**
     * @Route("/ajax/myroute", name="ajax_myroute", options={"expose"=true})
     */
    public function myRouteAction(Request $request)
    {
        // my code
    }

Then within a JS file

const routes = require('../../../web/js/fos_js_routes.json');
import Routing from '../../../vendor/friendsofsymfony/jsrouting-bundle/Resources/public/js/router.min.js';
Routing.setRoutingData(routes);

    $.ajax({
        url: Routing.generate('ajax_myroute'),
        method: "POST",
        data: data,
        success: function (response) {
            // some code
        }
    });
stof commented 6 years ago

well, when dumping routes, the base path is included in the dumped file too. So this requires configuring the router properly in the CLI (the incoming request cannot be used to guess it), as explained in the doc of the bundle command, which links to http://symfony.com/doc/4.1/console/request_context.html#configuring-the-request-context-globally

If your dev env is relying on having /app_dev.php in the URL, you need to router.request_context.base_url param (but not in the base_path one, which is only the base path to the document root)

picks44 commented 6 years ago

So adding the following in config_dev.yml should solve this issue right?

parameters:
    router.request_context.base_url: /app_dev.php
picks44 commented 6 years ago

Working thanks!