【问题标题】:Why i need use substr php function to escape : colon in Slim index.php route parameter为什么我需要使用 substr php 函数来转义:Slim index.php 路由参数中的冒号
【发布时间】:2015-02-22 07:14:17
【问题描述】:

当我使用 php 函数 substr($id,1) 转义参数冒号 (:id) 时,我的苗条路线 (index.php) 仅将数据返回到 angular (app.js)。

当我注释 substr 函数时,返回为 false,因为参数 id 之前的冒号。

为什么会这样?

app.js(角度)

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

【问题讨论】:

    标签: php angularjs rest slim


    【解决方案1】:

    我在 html href 上发现了问题:

    之前

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

    在我使用 ng-href 后没有冒号

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-25
      • 1970-01-01
      • 2017-11-07
      • 2011-11-20
      • 2011-04-22
      • 2020-10-24
      • 1970-01-01
      相关资源
      最近更新 更多