Symfony2 same path differents HTTP Methods - angularjs

I'm using angularjs with symfony2. I can't use the same path with a different method.
Angularjs service
angular.module('adminApp')
.service('users', ['$resource', function events($resource) {
function
return $resource(Routing.generate('users'), { id: "#_id" }, {
'create': { method: 'POST' },
'get': { method: 'GET', isArray: false },
'query': { method: 'GET', isArray: true },
'update': { method: 'PUT' },
'delete': { method: 'DELETE'}
});
}]);
Symfony2 Routing
users:
pattern: /users
defaults: { _controller: AppAdminBundle:User:list }
methods: [GET]
options:
expose: true
users:
pattern: /users
defaults: { _controller: AppAdminBundle:User:update }
methods: [PUT]
options:
expose: true
User controller
public function listAction()
{
//list
}
public function updateAction()
{
//update
}

You have two routes named equally.
Name first as users_list, second - users_update and it should work fine.

Related

How to make the URL of $resource generic?

I am new to AngularJS and I have a question here.
I am using $resource for my CRUD actions.
I currently have the code like this,
angular.module("dopAngular.services")
.factory("UserRoleService", ["$resource",
function ($resource) {
return $resource("api/UserRoleApi", {}, {
query: { method: "GET", isArray: true },
create: { method: "POST" },
get: { method: "GET" },
remove: { method: "DELETE" },
update: { method: "PUT" }
});
}]);
//below is the code in my controller
UserRoleService.query(function (data) {
vm.UserRoleLookups = data;
});
I would like to make my UserRoleService generic, which means I don't want to provide the specific URL for the API in the factory level.
I now modify my code a little bit,
angular.module("dopAngular.services")
.factory("UserRoleService", ["$resource",
function ($resource, url) {
return $resource(url, {}, {
query: { method: "GET", isArray: true },
create: { method: "POST" },
get: { method: "GET" },
remove: { method: "DELETE" },
update: { method: "PUT" }
});
}]);
My question is what I should do in my controller?
So, instead of directly returning $resource, we can encapsulate it with a function that accepts url as a param.
Something like this:
myApp.factory('UserRoleService', function($resource) {
return {
query: function(url) {
return $resource(url, {}, {
query: {
method: "GET",
isArray: true
},
get: {
method: "GET"
}
});
}
}
});
Now, in controller, you can call it like:
UserRoleService.query('//httpbin.org').get()
example fiddle

How to pass parameter to WebApi via $resource?

I'm trying to pass parameter to WebAPI, but something doesn't work. Not sure if I should add :id to the $resource path or not?
My service:
dataService.factory('Widgets', ['$resource', function ($resource) {
var data = $resource('/api/:path', {
path: '#path'
}, {
getWidgets: {
params: { path: 'widgets' },
method: "GET",
isArray: true
},
getWidget: {
params: { path: 'widgets' },
method: "GET",
isArray: true
},
getCategories: {
params: { path: 'widgetCategories' },
method: "GET",
isArray: true
},
});
return data;
}]);
In the controller:
Widgets.getWidget({ id: $rootScope.widgetId }).$promise.then(function (result) {
$scope.widget = result;
});
In ASP.Net Controller:
[HttpGet]
public IEnumerable<Widget> Get()
{
return _dbContext.Widgets;
}
[Route("api/[controller]/{id}")]
[HttpGet]
public Widget Get(int id)
{
return _dbContext.Widgets.Where(w => w.Id == id).FirstOrDefault(); ;
}
Could someone have a look please?

object is null in angularjs using $resource

In my resources i have following code
app.factory('myResource', function ($resource, $http) {
var resource = $resource('http://localhost:1234/api/:pg/:obj', {
pg: '#pg',
obj: '#obj'
},
{
'put': {
method: 'PUT'
},
'get': { method: 'GET' },
'save': { method: 'POST' },
'query': { method: 'GET', isArray: true },
'remove': { method: 'DELETE' },
'delete': { method: 'DELETE' }
}
);
return resource;
});
and in controller i use like
var param = { pg: 'MyTask', obj: taskModel }
$scope.TaskId = myResource.put(param);
and on debugging data is fine in obj but getting null in model
public HttpResponseMessage put(MyTask model)
{}
Your resource is configured to serialize #pg and #obj into URI.
$resource('http://localhost:1234/api/:pg/:obj', {
But WEBAPI sees MyTask object(complex object) and expects it to be from request body.
Either change client side to include "obj" data into request body or change server-side to get model from uri using webapi attributes. It all depends on complexity of "obj" information.

No data returned in consuming REStful web service using Angularjs

I am beginner learning Angularjs .Please help me with examples for following
script added
javascript -
var app = angular.module('myapp', []);
app.controller('MyCtrl1', ['$scope', 'UserFactory', function ($scope, UserFactory) {
UserFactory.get({}, function (userFactory) {
$scope.time = userFactory.time;
})
}]);
var service = angular.module('apiService', ['ngResource']);
service.factory('UserFactory', function ($resource) {
return $resource('http://time.jsontest.com', {}, {
query: {
method: 'GET',
params: {},
isArray: true
}
})
});
.html file
<body ng-app="myapp">
<divng-controller="MyCtrl1" >
<p>
Result from RESTful service is: {{ time }}
</p>
</div>
</body>
above snippet gives the out put as
Result from RESTful service is : {{time}}
and not the value i am expecting
..Reference : http://draptik.github.io/blog/2013/07/13/angularjs-example-using-a-java-restful-web-service/
I want to write CRUD methods (GET/POST/PUT/DELETE) and I have started with GET.
Thanks
You need to make sure that your main app module injects your service. In your plnkr you have:
var app = angular.module('myapp', []);
where you should really have:
var app = angular.module('myapp', ['apiService']);
This ensures that the service module is injected into your app module, and you can use the UserFactory that you define in that module. For this simple case you could have also simply defined the UserFactory factory on the 'myapp' module as well
It's very close but you have a slight mistake in your app instantiation. It should be the following:
var app = angular.module('myapp', [ 'apiService' ]);
There's a couple other issues I see as well but one thing is I usually do the following for async requests
var promise = UserFactory.get({}).$promise;
promise
.then( function(response) {
$scope.time = userFactory.time;
});
EDIT: Here's an example for named methods for a given ReST service:
return $resource('/api/v2.0/user/lists/:listId',
{},
{
// POST - list create/product addition to list
'addProduct': {
method: 'POST',
isArray: false,
params: {
listId: '#listId',
productId: '#productId'
}
},
'createList': {
method: 'POST',
isArray: false,
params: {
listName: '#listName'
}
},
// GET - list of user lists/list details
'readLists': {
method: 'GET',
isArray: false,
params: {}
},
'readListsWithProductId': {
method: 'GET',
isArray: false,
params: {
productId: '#productId'
}
},
'readListById': {
method: 'GET',
isArray: false,
params: {
listId: '#listId',
sort: '#sort',
flags: true,
extendedInfo: true,
rows: '#rows',
start: '#start'
}
},
// PUT - list renaming
'renameList': {
method: 'PUT',
isArray: false,
params: {
newName: '#listName',
listId: '#listId'
}
},
// DELETE - list deletion/clear/product removal
'removeProduct': {
method: 'DELETE',
isArray: false,
params: {
listId: '#listId',
productId: '#productId'
}
},
'clearList': {
method: 'DELETE',
isArray: false,
params: {
listId: '#listId',
clear: true
}
},
'deleteList': {
method: 'DELETE',
isArray: false,
params: {
listId: '#listId'
}
}
});
You could access it like the following:
Factory.[methodName](payload)

Generate Symfony Url in angular resource using FosJsBundle

I want genereate url in angular resource using FOSJSBundle.
This is the code that need FOSJSBundle to generate url:
Routing.generate('get_constructionprivateinformation_industrialists', {id: id}, false));
This is the service code that use resource:
services.factory('IndustrialistsFactory', function ($resource) {
return $resource(
'/app.php/api/v1/constructionprivateinformations/:id/industrialists',
{id: '#id'},
{
query: { method: 'GET', isArray: true },
create: { method: 'POST'}
}
)
});
This code works but I want generate url using FOSJSBundle, then If I do this code not works because Routing.generate have not access to id.
I proved this code but not works:
services.factory('IndustrialistsFactory', function ($resource) {
return $resource(
Routing.generate('get_constructionprivateinformation_industrialists', {id: id}),
{id: '#id'},
{
query: { method: 'GET', isArray: true },
create: { method: 'POST'}
}
)
});
What Can I do?
Thanks

Resources