Laravel is a great framework for developing php-based website and application, but for just creating an APIs, laravel can be overkill, to many features that you are not going to use, we need something simple.
Introducing slim framework, a php framework that focus on being simple and lightweight, perfect for creating APIs and web application.
To install slim framework on lubuntu basically you need to have composer, php and apache (or nginx) web server installed.
Slim framework requirements
How to install Slim 3 framework (create slim project) on lubuntu
create new folder for slim (for example "slim")
run composer command (wait until it finish)
navigate to the slim directory
create new folder called public
create new file index.php inside public directory, with the following code
create .htaccess file inside public folder:
create new virtual host pointing to /var/www/html/slim/public/, take a look at my article about how to setup virtual host on apache web server, the configuration file for the virtual host should be like this:
open url of the virtual host, let's say url is slim.local/ and then access slim.local/hello/lubuntu
To install slim framework on lubuntu basically you need to have composer, php and apache (or nginx) web server installed.
Slim framework requirements
- composer
- php
- web server (apache/nginx)
How to install Slim 3 framework (create slim project) on lubuntu
- open command line, navigate to the your web server root dir (for example /var/www/html/)
cd /var/www/html/
mkdir slim
sudo composer require slim/slim "^3.0"
cd slim
mkdir public
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
require '../vendor/autoload.php';
$app = new \Slim\App;
$app->get('/', function() {
return 'Hello World';
});
$app->get('/hello/{name}', function (Request $request, Response $response) {
$name = $request->getAttribute('name');
$response->getBody()->write("Hello, $name");
return $response;
});
$app->run();
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
<VirtualHost *:80>
ServerName slim.local
DocumentRoot /var/www/html/slim/public/
<Directory /var/www/html/slim/public/>
AllowOverride all
</Directory>
</VirtualHost>
Note that, as an alternative, you can also run php built in web server, so you don't need the apache web server, but this is just for testing purpose. You can run php built in web server like this:
php -S localhost:8999
Run the command above inside public directory (the same location as the index.php file), and then open url http://localhost:8999/ with your favorite web browser.
Congratulation, you have just create your first slim framework project, for more info about slim framework, feel free to check out the documentation on the official site.
Congratulation, you have just create your first slim framework project, for more info about slim framework, feel free to check out the documentation on the official site.
No comments:
Post a Comment