From ede9ffb8df2b225fb2deaea958f9cc126f3352b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Gia=20Phong?= Date: Sun, 11 Oct 2020 21:37:18 +0700 Subject: [PATCH] Implement a simple RESTful API --- app/Config/Routes.php | 2 +- app/Controllers/Home.php | 21 ++++++++++++++++----- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 56839ca..312d31e 100644 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -30,7 +30,7 @@ $routes->setAutoRoute(true); // We get a performance increase by specifying the default // route since we don't have to scan directories. -$routes->get('/', 'Home::index'); +$routes->get('/(:any)', 'Home::index/$1'); /** * -------------------------------------------------------------------- diff --git a/app/Controllers/Home.php b/app/Controllers/Home.php index 8798cdd..3c80328 100644 --- a/app/Controllers/Home.php +++ b/app/Controllers/Home.php @@ -1,12 +1,23 @@ /user

', '

/user/{userId}/post

', + '

/post

', '

/post/{postId}/comment

']; + + public function index($uri) { - return view('welcome_message'); + if (!file_exists($file = data($uri))) { + echo "

Supported URIs:

\n"; + return join("\n", $this->supported); + } + $this->response->setHeader('Access-Control-Allow-Origin', '*'); + $this->response->setContentType('application/json'); + readfile($file); } - - //-------------------------------------------------------------------- - } -- 2.28.0