0

I am using the FOSRestBundle, this bundle generates routes for me and pluralises those routes. For instance a GET request to /users.json is different from a GET request to /user/15.json

Its worth noting that a call to /users/15.json fails.

More on this issue here https://github.com/FriendsOfSymfony/FOSRestBundle/issues/247

In my Angular app I use a $resource to create a RESTful call, the URL is a template as detailed here https://docs.angularjs.org/api/ngResource/service/$resource

For example

$resource('http://example.com/:id.json')

Or

$resource('http://example.com/user/:id.json')

And there in is the problem, the $resource seems to accept a single URL template for the REST resource, but I have multiple ones because of the forced pluralisation from the FOSRestBundle.

I do not think hacking the FOSRestBundle is the answer, so what can I do with my usage of $resource in AngularJs to correct this issue?

3
  • 1
    You can do with $resource whatever you want, i.e.: $resource('example.com/:mode:id.json', {id : '@id'}, query : {method:'GET', params: {mode:'users', id : ''}, isArray:true}), Commented Sep 17, 2015 at 18:22
  • @PetrAveryanov but my mode still needs to be set for each request depending on if its for a single entity or multiple entities. Simply setting mode to users doesn't help - I need to dynamically set it somehow depending on the request requirement. Commented Sep 17, 2015 at 18:48
  • 1
    If you don't like the way FOSRestBundle automatically manages the routes there's nothing stopping you from setting them up to work the way you want, or any real reason why you shouldn't. Commented Sep 18, 2015 at 2:35

1 Answer 1

1

you can set url for every method as third parameter - actions

angular.module("example", ["ngResource"])
  .factory("userService", function($resource) {
    return $resource("http://example.com/user/:id.json", {
      id: "@id"
    }, {
      'query': {
        url: "http://example.com/users"
      }
    })
  })
  .run(function(userService) {
    userService.query();

    userService.get({id:1});
  })
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for this, its good to know. But, gah, seems so heavy handed and unnecessary given all the greatness that these two frameworks provide.
This has helped a lot now that I have tried it and changed my routes slightly. But what does this part do - id: "@id" - when removing it my code still works?
Sorry, it should be second parameter. I fix it. It means "as parameter id (key in object) will use id from passed object/entity"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.