My slim route (index.php) only return data to angular (app.js) when i escape parameter colon (:id) using php function substr($id,1).
When i comment the substr function the return is false because the colon before parameter id.
Why this?
app.js (angular)
var app = angular.module('app',['ngRoute']);
app.config(['$routeProvider',function($routeProvider) {
$routeProvider.
when('/',{controller: 'ListCtrl',templateUrl:'partials/list.html'}).
when('/product/:param',{controller:'ListIdCtrl',templateUrl:'partials/list.html'}).
otherwise({redirectTo:'/'});
}]);
app.controller('ListIdCtrl',['$scope','$http','$routeParams', function($scope,$http,$routeParams){
var requestId = $routeParams.param;
console.log(requestId);
$http.get("../product/" + requestId).success(function(data) {
console.log(data); //return false
}).
error(function(){
console.log('Error on get json');
});
}]);
index.php (slim)
require 'lib/Slim/Slim.php'; //include the framework in the project
\Slim\Slim::registerAutoloader(); //register the autoloader
$app = new \Slim\Slim(array());
$app->get('/product/:id','getProduct');
$app->run();
function getProduct($id) {
$sql = "select * from wines where id =:id";
try {
$db = getConnection();
$stmt = $db->prepare($sql);
$id = substr($id, 1); //need to use substr to remove colon on :id
$stmt->bindParam(":id", $id, PDO::PARAM_INT);
$stmt->execute();
$wine = $stmt->fetchObject();
$db = null;
echo json_encode($wine);
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
function getConnection() {
return $dbh;
}