Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $dbparams = array(
- 'host' => 'localhost',
- 'user' => '********',
- 'password' => '************',
- 'dbname' => 'angularjs',
- );
- <?php
- //nécessite Slim
- require 'vendor/autoload.php';
- require 'config/config.php';
- //Instancie une app de Slim
- $app = new SlimApp;
- //Associe type de requête avec fonction et paramêtres
- $app->get('/items', 'getItems');
- $app->get('/items/{id:d+}', 'getItemById');
- //Démarre l'application
- $app->run();
- // Accès à la base de données
- function DB_Connection() {
- $dbhost = $dbparams['host'];
- $dbuser = $dbparams['user'];
- $dbpass = $dbparams['password'];
- $dbname = $dbparams['dbname'];
- $dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
- $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- return $dbh;
- }
- function getItems() {
- $sql = "select * FROM aj_items";
- try {
- $db = DB_Connection();
- $stmt = $db->query($sql);
- $list = $stmt->fetchAll(PDO::FETCH_OBJ);
- $db = null;
- echo json_encode($list);
- } catch(PDOException $e) {
- echo '{"error":{"text":'. $e->getMessage() .'}}';
- }
- }
- function getItemById($req, $resp, $args) {
- $id = $args["id"];
- $sql = "select * FROM aj_items WHERE id=".$id;
- try {
- $db = DB_Connection();
- $stmt = $db->query($sql);
- $list = $stmt->fetchAll(PDO::FETCH_OBJ);
- $db = null;
- echo json_encode($list);
- } catch(PDOException $e) {
- echo '{"error":{"text":'. $e->getMessage() .'}}';
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement