ak85
ak85

Reputation: 4264

Use the same function for multiple get requests slim framework

I am trying to return some json data using the slim frame work.

I have the below data

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

Sample data

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

My php code looks like the below

$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() .'}}'; 
    }
}

When I go to the below url

http://localhost/api/students

I get this returned which is expected.

{"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"}
    ]
}

I want to add some potential filters to the request. eg filter by gender

$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() .'}}'; 
    }
}

If I return

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

I get this returned which is expected.

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

However if I try

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}

I am guessing in my syntax

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

The second line is over writing the first line?

How can I set this up so I can optionally pass params into my request, whilst using the same function?

Upvotes: 0

Views: 1662

Answers (1)

ʰᵈˑ
ʰᵈˑ

Reputation: 11375

You need to set up a default value for your function parameter.

function getStudents($gender = "") 

Upvotes: 1

Related Questions