NucleusXorg
NucleusXorg

Reputation: 23

Send a JS variable to a specific Symfony route

I have a Symfony 3 project with Angular JS in front. The project is about taking an URL with specific XML content, send it to SF3, do some manipulation, and send the final result to Angular JS.

I just want to catch my url variable in my Symfony Controller.

app.js

var app = angular.module("test", []);


app.controller("testCtrl", ['$scope', '$http', '$window', function($scope, $http, $window) {


    $scope.getMyXML = function() {


        $http.get($scope.urlSubmitted)
            .success(function(data, status, headers, config) {


                $http({
                    method: 'POST',
                    url: Routing.generate('f_api'), //FOSJsRoutingBundle
                    headers: {},
                    params: {},
                    data: {urlToParse:$scope.urlSubmitted}
                }).then(function successCallback(response) {
                    console.log('Sended to SF3');
                }, function errorCallback(response) {
                    console.log('NOT sended to sf2');
                });


            })
            .error(function(data, status, headers, config) {
                alert('!! XML GET ERROR !!');
            })
    }

}]);

Symfony controller action

class MyController extends Controller { public function indexAction(Request $request) {

    $serializer = $this->get('jms_serializer');
    $data = $request->request->get('urlToParse');
    //var_dump($data);
    //die;





    return $this->render('MyBundle:My:index.html.twig');
}

public function apiAction(Request $request)
{

    $data = $request->request->get('urlToParse');
    var_dump($data);
    die;

    return new JsonResponse();
}

}

And last, my routing.yml

    f_home:
    path:     /
    defaults: { _controller: MyBundle:My:index }

f_api:
    path:     /api/
    defaults: { _controller: MyBundle:My:api }
    options:
            expose: true

The apiAction is never lanched, despite of the angular http post on it...

Thanks !

Upvotes: 0

Views: 296

Answers (1)

Besbes Riadh
Besbes Riadh

Reputation: 851

Try this:

data: {'urlToParse':$scope.urlSubmitted}

then:

$data = $request->request->get('urlToParse');

Upvotes: 0

Related Questions