Created
February 20, 2013 12:38
-
-
Save wouterds/4995246 to your computer and use it in GitHub Desktop.
Slim framework used in a class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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