为什么我需要使用substr php函数来转义:Slim index.php route参数中的冒号

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;
}

1个回答

I found the problem on html href:

before

<a class="text-link" href="#/product/:{{vinho.id}}" ><h4>{{vinho.username}}</h4></a>

after i use ng-href withou colon

<a class="text-link" ng-href="#/product/{{vinho.id}}" ><h4>{{vinho.username}}</h4></a>
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐