Cài đặt Slim Framework
Yêu cầu hệ thống:
Web server with URL rewriting
PHP 7.2 or newer
Ở đây mình dùng xampp và Slim version 4.Các bạn download ở đây
Tạo project tên api
composer create-project slim/slim-skeleton api
Tạo file .htaccess ở thư mục api
RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule (.+) index.php?p=$1 [QSA,L]
Tạo file index.php ở thư mục api
//api/index.php <?php use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; use Slim\Factory\AppFactory; require __DIR__ . '/vendor/autoload.php'; $app = AppFactory::create(); // Add Error Handling Middleware $app->addErrorMiddleware(true, false, false); $app->setBasePath('/api'); $app->get('/', function (Request $request, Response $response, $args) { $response->getBody()->write("Hello world!"); return $response; }); $app->run();
Truy cập link http://localhost/api/ nếu thấy dòng chữ Hello world! là ok.