pull/8/head
sundowndev 2018-07-22 02:32:40 +02:00
parent 5d83adb0b3
commit c176e32f89
1 changed files with 60 additions and 7 deletions

View File

@ -1,32 +1,85 @@
<?php
use App\Service\DotEnvParser;
use App\Service\JsonResponse;
$router->setNamespace('\App\Controller');
$router->before('GET|POST', '/.*', function() use ($router) {
$router->before('GET|POST', '/.*', function () use ($router) {
# This will be always executed
$dotEnvParser = new DotEnvParser();
$dotEnvParser->run();
$jsonResponse = new JsonResponse();
if ($_SERVER['HTTP_ACCEPT'] !== 'application/json') {
$code = 400;
$message = 'Accept header is not set to "application/json".';
print $jsonResponse->create($code, $message, []);
die();
} elseif ($_SERVER['REQUEST_METHOD'] != 'GET' && $_SERVER['CONTENT_TYPE'] !== 'application/json') {
$code = 400;
$message = 'Content-type header is not set to "application/json".';
print $jsonResponse->create($code, $message, []);
die();
}/*elseif($_SERVER['HTTP_ORIGIN'] !== getenv('ALLOW_ORIGIN')) {
$code = 403;
$message = 'Unallowed cross origin.';
print $jsonResponse->create($code, $message, []);
die();
}*/
});
/**
* API index
*/
$router->get('/', 'DefaultController@index');
/**
* Session handling routes
*/
$router->post('/auth', 'SessionController@auth');
$router->post('/register', 'SessionController@signup');
$router->post('/logout', 'SessionController@signout');
/**
* Task resource
*/
$router->mount('/tasks', function () use ($router) {
// get all tasks
// Get all tasks
$router->get('/', 'TaskController@getAll');
// get one task
// Get one task
$router->get('/(\d+)', 'TaskController@get');
// create a task
// Create a task
$router->post('/', 'TaskController@post');
// update a task
// Update a task
$router->put('/(\d+)', 'TaskController@put');
// delete a task
// Delete a task
$router->delete('/(\d+)', 'TaskController@delete');
});
/**
* User resource
*/
$router->mount('/users', function () use ($router) {
// endpoints for user
// Create user (register)
$router->post('/', 'DefaultController@index');
// Get your own account data
$router->get('/me', 'DefaultController@index');
// Get one user
$router->get('/(\d+)', 'DefaultController@index');
// Get one task's tasks
$router->get('/(\d+)/tasks', 'DefaultController@index');
});
/**
* 404 error response
*/
$router->set404('DefaultController@error');