【问题标题】:Use the same function for multiple get requests slim framework对多个 get 请求使用相同的功能 slim 框架
【发布时间】:2014-10-13 09:00:20
【问题描述】:

我正在尝试使用超薄框架返回一些 json 数据。

我有以下数据

CREATE TABLE students (
    id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(255),
    gender INT
) DEFAULT CHARACTER SET utf8 ENGINE=InnoDB;

样本数据

INSERT INTO students (id, name, gender) VALUES
(1, "John", 1),
(2, "Kate", 2),
(3, "Sarah",2),
(4, "Michelle", 2),
(5, "Scott", 1);

我的 php 代码如下所示

$app = new Slim();
$app->get('/students', 'getStudents');
$app->run();


function getStudents() {

    $sql = "SELECT * FROM students ORDER BY id";
    try {
        $db = getConnection();
        $stmt = $db->query($sql); 
        $students = $stmt->fetchAll(PDO::FETCH_OBJ);
        $db = null;
        echo '{"students": ' . json_encode($students) . '}';
    } catch(PDOException $e) {
        echo '{"error":{"text":'. $e->getMessage() .'}}'; 
    }
}

当我转到以下网址时

http://localhost/api/students

我得到了预期的返回。

{"students": 
    [
        {"id":"1","name":"John","gender":"1"},
        {"id":"2","name":"Kate","gender":"2"},
        {"id":"3","name":"Sarah","gender":"2"},
        {"id":"4","name":"Michelle","gender":"2"},
        {"id":"5","name":"Scott","gender":"1"}
    ]
}

我想为请求添加一些潜在的过滤器。 例如按性别过滤

$app = new Slim();
$app->get('/students', 'getStudents');
$app->get('/students/gender/:gender', 'getStudents');
$app->run();


function getStudents($gender) {
    $filterGender = '';
    if($gender) {
       $filterGender = "WHERE gender =" . $gender;
    }
    $sql = "SELECT * FROM students " . $filterGender . " ORDER BY id";
    try {
        $db = getConnection();
        $stmt = $db->prepare($sql);
        if($gender) {        
            $stmt->bindParam("gender", $gender, PDO::PARAM_INT);
        }
        $stmt->execute();        
        $students = $stmt->fetchAll(PDO::FETCH_OBJ);
        $db = null;       
        echo '{"students": ' . json_encode($students) . '}';
    } catch(PDOException $e) {
        echo '{"error":{"text":'. $e->getMessage() .'}}'; 
    }
}

如果我回来

http://localhost/api/students/gender/1

我得到了预期的返回。

{"students": 
    [
        {"id":"1","name":"John","gender":"1"},
        {"id":"5","name":"Scott","gender":"1"}
    ]
}

但是如果我尝试

http://localhost/api/students

#1 [internal function]: getStudents()<br />
#2 \api\Slim\Route.php(392): call_user_func_array('getStudents', Array)<br />
#3 \api\Slim\Slim.php(1052): Slim_Route->dispatch()<br />
#4 \api\index.php(14): Slim->run()<br />
#5 {main}

我猜我的语法

    $app->get('/students', 'getStudents');
    $app->get('/students/gender/:gender', 'getStudents');

第二行写完第一行了?

如何进行设置,以便在使用相同功能的同时选择性地将参数传递到我的请求中?

【问题讨论】:

  • 我没用过slim,但你能试试function getStudents($gender = "")
  • @ʰᵈˑ 这似乎确实适用于我的测试用例谢谢。我确实在函数之外尝试了类似的东西,但无法让它工作。
  • 太棒了。由于它解决了您的问题,因此我已将其发布为答案。

标签: php function rest get slim


【解决方案1】:

你需要为你的函数参数设置一个默认值。

function getStudents($gender = "") 

【讨论】:

    猜你喜欢
    • 2014-02-18
    • 2013-06-12
    • 1970-01-01
    • 1970-01-01
    • 2015-10-24
    • 2014-08-27
    • 1970-01-01
    • 2015-03-13
    • 2012-07-16
    相关资源
    最近更新 更多