I'm new to AngularJS and I have trouble to update an object via REST. I'm using a PHP/Mysql Backend (Slim Framework).
I'm able to retrieve (GET), create (POST) a new object but not to edit (PUT) one. Here's the code:
my Form:
<form name="actionForm" novalidate ng-submit="submitAction();">
Name: <input type="text" ng-model="action.name" name="name" required>
<input type="submit">
</form>
My service:
var AppServices = angular.module('AppServices', ['ngResource'])
AppServices.factory('appFactory', function($resource) {
return $resource('/api/main/actions/:actionid', {}, {
'update': { method: 'PUT'},
});
});
app.js
var app = angular.module('app', ['AppServices'])
app.config(function($routeProvider) {
$routeProvider.when('/main/actions', {
templateUrl: 'partials/main.html',
controller: 'ActionListCtrl'
});
$routeProvider.when('/main/actions/:actionid', {
templateUrl: 'partials/main.html',
controller: 'ActionDetailCtrl'
});
$routeProvider.otherwise({redirectTo: '/main/actions'});
});
controllers.js:
function ActionDetailCtrl($scope, $routeParams, appFactory, $location) {
$scope.action = appFactory.get({actionid: $routeParams.actionid});
$scope.addAction = function() {
$location.path("/main/actions/new");
}
$scope.submitAction = function() {
// UPDATE CASE
if ($scope.action.actionid > 0) {
$scope.action = appFactory.update($scope.action);
alert('Action "' + $scope.action.title + '" updated');
} else {
// CREATE CASE
$scope.action = appFactory.save($scope.action);
alert('Action "' + $scope.action.title + '" created');
}
$location.path("/main/actions");
}
}
In Slim, in api/index.php, I've got these routes and functions defined:
$app->get('/main/actions', 'getActions');
$app->get('/main/actions/:actionid', 'getAction');
$app->post('/main/actions', 'addAction');
$app->put('/main/actions/:actionid', 'updateAction');
When I create a new "action", everything is working as expected. But when I try to edit an existing one, I've got this error:
PUT http://project.local/api/main/actions 404 Not Found
The action is not updated (although the alert message "Action xxx updated" is displayed)
Is it an issue with my routeProvider setting ? I guess the PUT url misses the id at the end...
I precise that if I try to simulate the PUT request with POSTMan-Chrome-Extension for example, everything is working well (PUT http://project.local/api/main/actions/3 returns the expected data)