Created
February 29, 2016 19:25
-
-
Save internoma/db09d5e33354a4809c00 to your computer and use it in GitHub Desktop.
Minimal micro REST API PHP
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
| RewriteEngine On | |
| RewriteCond %{REQUEST_FILENAME} !-f | |
| RewriteCond %{REQUEST_FILENAME} !-d | |
| RewriteRule ^(.*)$ index.php [QSA,L] |
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
| [ | |
| { | |
| "id" : "18741DB9-B869-B378-8624-0B9F593F84BA", | |
| "name" : "Ross", | |
| "email" : "lobortis@arcuetpede.ca", | |
| "about" : "nisl. Quisque fringilla euismod enim.", | |
| "title" : "Mr." | |
| }, | |
| { | |
| "id" : "E2D7A39E-4583-A27A-BC55-98489F062FB1", | |
| "name" : "Bruno", | |
| "email" : "egestas.nunc@eratvitaerisus.edu", | |
| "about" : "Donec tincidunt. Donec vitae erat", | |
| "title" : "Ms." | |
| }, | |
| { | |
| "id" : "26CC1826-2BBA-5ABB-CB7E-D06AD96D1A23", | |
| "name" : "Audra", | |
| "email" : "tincidunt.vehicula.risus@tempus.com", | |
| "about" : "Nullam suscipit, est ac facilisis", | |
| "title" : "Mr." | |
| }, | |
| { | |
| "id" : "4F0FCC7A-A715-4AC2-B64F-830AA3441995", | |
| "name" : "Kelsey", | |
| "email" : "sodales.at@quis.net", | |
| "about" : "mi felis, adipiscing fringilla, porttitor", | |
| "title" : "Mr." | |
| }, | |
| { | |
| "id" : "8A3A6AF5-225D-D812-ABB0-3668D27F677F", | |
| "name" : "Blaine", | |
| "email" : "suscipit.est.ac@aclibero.ca", | |
| "about" : "lorem eu metus. In lorem.", | |
| "title" : "Mr." | |
| }, | |
| { | |
| "id" : "8B7A6266-5CDD-AF5A-6C15-ECF6273A26FB", | |
| "name" : "Brielle", | |
| "email" : "Morbi.metus@nislQuisque.net", | |
| "about" : "ullamcorper eu, euismod ac, fermentum", | |
| "title" : "Dr." | |
| }, | |
| { | |
| "id" : "3C1D2DF8-CB72-EF58-2B7D-DD266E54710A", | |
| "name" : "Susan", | |
| "email" : "rutrum@aliquet.org", | |
| "about" : "mattis. Cras eget nisi dictum", | |
| "title" : "Mrs." | |
| }, | |
| { | |
| "id" : "1F391F0E-DE13-5089-7A92-8BA9B1264245", | |
| "name" : "Brent", | |
| "email" : "nibh.dolor@magna.edu", | |
| "about" : "fringilla. Donec feugiat metus sit", | |
| "title" : "" | |
| }, | |
| { | |
| "id" : "340DCB7E-428E-EA04-E394-513893AE2E56", | |
| "name" : "Cally", | |
| "email" : "Cras.lorem@eunullaat.net", | |
| "about" : "nunc. In at pede. Cras", | |
| "title" : "" | |
| }, | |
| { | |
| "id" : "E2D7A39E-4583-A27A-BC55-98489F062FB1", | |
| "name" : "Bruno", | |
| "email" : "egestas.nunc@eratvitaerisus.edu", | |
| "about" : "Donec tincidunt. Donec vitae erat", | |
| "title" : "Ms." | |
| }, | |
| { | |
| "id" : "26CC1826-2BBA-5ABB-CB7E-D06AD96D1A23", | |
| "name" : "Audra", | |
| "email" : "tincidunt.vehicula.risus@tempus.com", | |
| "about" : "Nullam suscipit, est ac facilisis", | |
| "title" : "Mr." | |
| }, | |
| { | |
| "id" : "4F0FCC7A-A715-4AC2-B64F-830AA3441995", | |
| "name" : "Kelsey", | |
| "email" : "sodales.at@quis.net", | |
| "about" : "mi felis, adipiscing fringilla, porttitor", | |
| "title" : "Mr." | |
| }, | |
| { | |
| "id" : "8A3A6AF5-225D-D812-ABB0-3668D27F677F", | |
| "name" : "Blaine", | |
| "email" : "suscipit.est.ac@aclibero.ca", | |
| "about" : "lorem eu metus. In lorem.", | |
| "title" : "Mr." | |
| }, | |
| { | |
| "id" : "8B7A6266-5CDD-AF5A-6C15-ECF6273A26FB", | |
| "name" : "Brielle", | |
| "email" : "Morbi.metus@nislQuisque.net", | |
| "about" : "ullamcorper eu, euismod ac, fermentum", | |
| "title" : "Dr." | |
| }, | |
| { | |
| "id" : "3C1D2DF8-CB72-EF58-2B7D-DD266E54710A", | |
| "name" : "Susan", | |
| "email" : "rutrum@aliquet.org", | |
| "about" : "mattis. Cras eget nisi dictum", | |
| "title" : "Mrs." | |
| }, | |
| { | |
| "id" : "1F391F0E-DE13-5089-7A92-8BA9B1264245", | |
| "name" : "Brent", | |
| "email" : "nibh.dolor@magna.edu", | |
| "about" : "fringilla. Donec feugiat metus sit", | |
| "title" : "" | |
| }, | |
| { | |
| "id" : "340DCB7E-428E-EA04-E394-513893AE2E56", | |
| "name" : "Cally", | |
| "email" : "Cras.lorem@eunullaat.net", | |
| "about" : "nunc. In at pede. Cras", | |
| "title" : "" | |
| }, | |
| { | |
| "id" : "E2D7A39E-4583-A27A-BC55-98489F062FB1", | |
| "name" : "Bruno", | |
| "email" : "egestas.nunc@eratvitaerisus.edu", | |
| "about" : "Donec tincidunt. Donec vitae erat", | |
| "title" : "Ms." | |
| }, | |
| { | |
| "id" : "26CC1826-2BBA-5ABB-CB7E-D06AD96D1A23", | |
| "name" : "Audra", | |
| "email" : "tincidunt.vehicula.risus@tempus.com", | |
| "about" : "Nullam suscipit, est ac facilisis", | |
| "title" : "Mr." | |
| }, | |
| { | |
| "id" : "4F0FCC7A-A715-4AC2-B64F-830AA3441995", | |
| "name" : "Kelsey", | |
| "email" : "sodales.at@quis.net", | |
| "about" : "mi felis, adipiscing fringilla, porttitor", | |
| "title" : "Mr." | |
| }, | |
| { | |
| "id" : "8A3A6AF5-225D-D812-ABB0-3668D27F677F", | |
| "name" : "Blaine", | |
| "email" : "suscipit.est.ac@aclibero.ca", | |
| "about" : "lorem eu metus. In lorem.", | |
| "title" : "Mr." | |
| }, | |
| { | |
| "id" : "8B7A6266-5CDD-AF5A-6C15-ECF6273A26FB", | |
| "name" : "Brielle", | |
| "email" : "Morbi.metus@nislQuisque.net", | |
| "about" : "ullamcorper eu, euismod ac, fermentum", | |
| "title" : "Dr." | |
| }, | |
| { | |
| "id" : "3C1D2DF8-CB72-EF58-2B7D-DD266E54710A", | |
| "name" : "Susan", | |
| "email" : "rutrum@aliquet.org", | |
| "about" : "mattis. Cras eget nisi dictum", | |
| "title" : "Mrs." | |
| }, | |
| { | |
| "id" : "1F391F0E-DE13-5089-7A92-8BA9B1264245", | |
| "name" : "Brent", | |
| "email" : "nibh.dolor@magna.edu", | |
| "about" : "fringilla. Donec feugiat metus sit", | |
| "title" : "" | |
| }, | |
| { | |
| "id" : "340DCB7E-428E-EA04-E394-513893AE2E56", | |
| "name" : "Cally", | |
| "email" : "Cras.lorem@eunullaat.net", | |
| "about" : "nunc. In at pede. Cras", | |
| "title" : "" | |
| }, | |
| { | |
| "id" : "C061F9A3-EC77-6B24-E0A4-218134EA240E", | |
| "name" : "Edan", | |
| "email" : "vel.mauris@sed.ca", | |
| "about" : "sit amet orci. Ut sagittis", | |
| "title" : "Ms." | |
| } | |
| ] |
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 | |
| ini_set('display_errors', 1); | |
| error_reporting(E_ALL | E_STRICT); | |
| require_once 'RegexRouter.php'; | |
| $router = new RegexRouter(); | |
| /** | |
| * Routes | |
| * API - Rest | |
| */ | |
| $router->route('/^\/javascript\/underscore\/api\/(\w+)\/(\d+)\/?$/', function($category, $id){ | |
| header('Content-Type: application/json'); | |
| $data = file_get_contents("{$category}.json"); | |
| if ($id == 0) { | |
| echo $data; | |
| } else { | |
| $json = json_decode($data); | |
| echo json_encode($json[$id-1]); | |
| } | |
| }); | |
| $router->execute($_SERVER['REQUEST_URI']); |
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 | |
| class RegexRouter { | |
| private $routes = array(); | |
| public function route($pattern, $callback) { | |
| $this->routes[$pattern] = $callback; | |
| } | |
| public function execute($uri) { | |
| foreach ($this->routes as $pattern => $callback) { | |
| if (preg_match($pattern, $uri, $params) === 1) { | |
| array_shift($params); | |
| return call_user_func_array($callback, array_values($params)); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment