PHP RESTful API for a student repository All of the codes are based on the PSR-2 standard. Also Dependency-Injection and Repository patterns are followed. I would love to hear some advices or comments.

StudentRepository Class



namespace REST;

use Slim\PDO\Database;


interface IStudentRepository
{
    //Functions to be defined
    public function __construct();
    public function getStudentNames($request, $response, array $args);
    public function getAll($request, $response, array $args);
    public function addStudent($name, $id);
    public function updateName($id, $new_name);
}


class StudentRepository implements IStudentRepository
{
    //Queries and MYSQL connection
    protected $pdo;

public function __construct()
{
    //Database connection
    $dsn = 'mysql:host=localhost;dbname=College_DB;charset=utf8';
    $usr = 'root';
    $pwd = '';

    $this-&gt;pdo = new \Slim\PDO\Database($dsn, $usr, $pwd);
}

public function getStudentNames($request, $response, array $args)
{
    $array_val = array();
    $sql = 'SELECT name FROM Students';
    $cntrl=0;
    foreach ($this-&gt;pdo-&gt;query($sql) as $row) {
        $array_val[$cntrl]=$row["name"];
        $cntrl++;
    }
    $n_response = $response-&gt;withJson($array_val, 200);
    return $n_response;
}

public function getAll($request, $response, array $args)
{
    $array_val = array();
    $sql = 'SELECT * FROM Students';

    foreach ($this-&gt;pdo-&gt;query($sql) as $row) {
        $array_val[$row["id"]]=$row["name"];
    }

    $n_response = $response-&gt;withJson($array_val, 200);
    return $n_response;
}

public function addStudent($id, $name)
{

    $stmt = $this-&gt;pdo-&gt;prepare('INSERT into Students(id,name) values(:id,:namee)');
    $stmt-&gt;bindParam(":id", $id, \Slim\PDO\Database::PARAM_STR);
    $stmt-&gt;bindParam(":namee", $name, \Slim\PDO\Database::PARAM_STR);

    $stmt-&gt;execute();
}

public function updateName($id, $new_name)
{
    $stmt = $this-&gt;pdo-&gt;prepare('UPDATE Students set name=:namee where id=:id');
    $stmt-&gt;bindParam(":id", $id, \Slim\PDO\Database::PARAM_STR);
    $stmt-&gt;bindParam(":namee", $new_name, \Slim\PDO\Database::PARAM_STR);

    $stmt-&gt;execute();
}
}


StudentController Class

&lt;?php

namespace REST;

class StudentController
{
    protected $Student_DB;

public function __construct(IStudentRepository $Student_DB)
{
    $this-&gt;Student_DB=$Student_DB;
}

public function getStudents($request, $response, array $args)
{
    return $this-&gt;Student_DB-&gt;getAll($request, $response, $args);
}

public function getStudentNames($request, $response, array $args)
{
    return $this-&gt;Student_DB-&gt;getStudentNames($request, $response, $args);
}

public function addStudent($request, $response, array $args)
{
    if (array_key_exists("id", $args) &amp;&amp; array_key_exists("name", $args)) {
        $id = $args["id"];
        $name = $args["name"];
        if (!is_numeric($id) || !ctype_alpha($name)) {
            $n_response = $response-&gt;withStatus(400);
            return $n_response;
        }
    }
    $this-&gt;Student_DB-&gt;addStudent($id, $name);
    $n_response = $response-&gt;withStatus(200);
    return $n_response;
}
}


Edit : I have edited the code in order to return JSON data with spesific status codes.
The StudentRepository is all about database, and shouldn't know about $request and $response objects, that is the job of the controller.

What if you were fetching data to put in an excel file, then you can't use the StudentRepository as it was to fetch that data.

&lt;?php

namespace REST;

use Slim\PDO\Database;


interface IStudentRepository
{
    //Functions to be defined
    public function __construct(array $credentials);
    public function getStudentNames(array $args);
    public function getAll(array $args);
    public function addStudent($name, $id);
    public function updateName($id, $new_name);
}


class StudentRepository implements IStudentRepository
{
    //Queries and MYSQL connection
    protected $pdo;

    public function __construct(array $credentials)
    {
        // TODO configuration should be stored outside the class, in a config object or file
//        //Database connection
//        $dsn = 'mysql:host=localhost;dbname=College_DB;charset=utf8';
//        $usr = 'root';
//        $pwd = '';

        $this-&gt;pdo = new \Slim\PDO\Database($credentials['dsn'], $credentials['usr'], $credentials['pwd']);
    }

    public function getStudentNames(array $args)
    {
        // TODO recommend you use ORDER BY to get a consistent order from request, to request
        $sql = 'SELECT name FROM Students';

        $array_val = array();

        foreach ($this-&gt;pdo-&gt;query($sql) as $row) {
            $array_val[]=$row["name"];
        }

        return $array_val;
    }

    public function getAll(array $args)
    {
        // TODO recommend you use ORDER BY to get a consistent order from request, to request
        $sql = 'SELECT * FROM Students';

        $array_val = array();

        foreach ($this-&gt;pdo-&gt;query($sql) as $row) {
            $array_val[$row["id"]]=$row["name"];
        }

        return $array_val;
    }

    public function addStudent($id, $name)
    {
        $stmt = $this-&gt;pdo-&gt;prepare('INSERT into Students(id,name) values(:id,:namee)');
        $stmt-&gt;bindParam(":id", $id, \Slim\PDO\Database::PARAM_STR);
        $stmt-&gt;bindParam(":namee", $name, \Slim\PDO\Database::PARAM_STR);

        $stmt-&gt;execute();
    }

    public function updateName($id, $new_name)
    {
        $stmt = $this-&gt;pdo-&gt;prepare('UPDATE Students set name=:namee where id=:id');
        $stmt-&gt;bindParam(":id", $id, \Slim\PDO\Database::PARAM_STR);
        $stmt-&gt;bindParam(":namee", $new_name, \Slim\PDO\Database::PARAM_STR);

        $stmt-&gt;execute();
    }
}
?&gt;

&lt;?php

namespace REST;

class StudentController
{
    protected $Student_DB;

    public function __construct(IStudentRepository $Student_DB)
    {
        $this-&gt;Student_DB=$Student_DB;
    }

    public function getStudents($request, $response, array $args)
    {
        $all = $this-&gt;Student_DB-&gt;getAll();
        return $response-&gt;withJson($all, 200);
    }

    public function getStudentNames($request, $response, array $args)
    {
        $names = $this-&gt;Student_DB-&gt;getStudentNames();
        return $response-&gt;withJson($names, 200);
    }

    public function addStudent($request, $response, array $args)
    {

        // improve tests, as it was possible for $id and $name to be undefined with previous tests
        if (!array_key_exists("id", $args) || !array_key_exists("name", $args)) {
            return $response-&gt;withStatus(400);
        }

        $id = $args["id"];
        $name = $args["name"];

        if (!is_numeric($id) || !ctype_alpha($name)) {
            return $response-&gt;withStatus(400);
        }

        $this-&gt;Student_DB-&gt;addStudent($id, $name);
        return $response-&gt;withStatus(200);
    }
}