Skip to content

Instantly share code, notes, and snippets.

@wouterds
Created February 20, 2013 12:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wouterds/4995246 to your computer and use it in GitHub Desktop.
Save wouterds/4995246 to your computer and use it in GitHub Desktop.
Slim framework used in a class
<?php
session_start();
define('WWW_ROOT', dirname(__FILE__) . DIRECTORY_SEPARATOR);
require_once WWW_ROOT . 'includes' . DIRECTORY_SEPARATOR . 'functions.php';
require_once WWW_ROOT . 'classes' . DIRECTORY_SEPARATOR . 'Config.php';
require_once(WWW_ROOT . 'dao' . DIRECTORY_SEPARATOR . 'UserDAO.php');
require_once(WWW_ROOT . 'dao' . DIRECTORY_SEPARATOR . 'EventDAO.php');
require_once(WWW_ROOT . 'dao' . DIRECTORY_SEPARATOR . 'RegistrationDAO.php');
require_once(WWW_ROOT . 'Slim' . DIRECTORY_SEPARATOR . 'Slim.php');
class api {
private $app;
private $userDao;
private $eventDao;
private $registrationDao;
private $feedback = array("status" => "error", "response" => "Invalid or no parameters given.");
function __construct() {
$this->userDao = new UserDAO();
$this->eventDao = new EventDAO();
$this->registrationDao = new RegistrationDAO();
$this->app = new Slim();
$this->app->get('/', array($this, 'index'));
$this->app->post('/users/login', array($this, 'login'));
$this->app->post('/users/register', array($this, 'addUser'));
$this->app->get('/events', array($this, 'events'));
$this->app->get('/events/id', array($this, 'eventById'));
$this->app->post('/events/add', array($this, 'addEvent'));
$this->app->post('/events/update/id', array($this, 'updateEventById'));
$this->app->get('/events/delete/id', array($this, 'deleteEventById'));
$this->app->get("/registrations/eventid", array($this, 'registrationById'));
$this->app->get("/registrations/delete/id", array($this, 'registrationDeleteById'));
$this->app->post("/registrations/add", array($this, 'addRegistration'));
$this->app->run();
}
public function index() {
$this->display();
}
public function addRegistration() {
$request = $this->app->request()->post();
$this->feedback = array("status" => "success", "response" => $this->registrationDao->addRegistrationForEvent($request));
$this->display();
}
public function addEvent() {
$request = $this->app->request()->post();
$this->feedback = array("status" => "success", "response" => $this->eventDao->addEvent($request));
$this->display();
}
public function updateEventById() {
$request = $this->app->request()->post();
if(isset($_POST['id']))
$this->feedback = array("status" => "success", "response" => $this->eventDao->updateEvent($_POST['id'], $request));
$this->display();
}
public function deleteEventById() {
if(isset($_GET['id']) && isset($_GET['api-key']))
$this->feedback = array("status" => "success", "response" => $this->feedback = $this->eventDao->deleteEventById($_GET['id'], $_GET['api-key']));
$this->display();
}
public function registrationDeleteById() {
if(isset($_GET['id']))
$this->feedback = array("status" => "success", "response" => $this->feedback = $this->registrationDao->deleteRegistrationForEvent($_GET['id']));
$this->display();
}
public function events() {
if(isset($_GET['api-key']))
$this->feedback = $this->eventDao->getEventsByUser($_GET['api-key']);
$this->display();
}
public function registrationById() {
if(isset($_GET['id']))
$this->feedback = array("status" => "success", "response" => $this->registrationDao->getRegistrationsForEvent($_GET['id']));
$this->display();
}
public function eventById() {
if(isset($_GET['id']) && isset($_GET['api-key']))
$this->feedback = $this->eventDao->getEventById($_GET['id'], $_GET['api-key']);
$this->display();
}
public function addUser() {
$request = $this->app->request()->post();
$this->feedback = array("status" => "success", "response" => $this->userDao->addUser($request));
$this->display();
}
public function login() {
$request = $this->app->request()->post();
$this->feedback = array("status" => "success", "response" => $this->userDao->login($request));
$this->display();
}
public function display() {
die(json_encode($this->feedback));
}
}
new api();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment