Wednesday, May 3, 2017

How to install Slim 3 Framework on Lubuntu

Slim Framework on Lubuntu

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
  1. composer
  2. php
  3. web server (apache/nginx)
I'm not going to show you how to install composer, php or apache web server in here, because i already cover that on my other article, please check them if you haven't.

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/
  • create new folder for slim (for example "slim")
  • mkdir slim
  • run composer command (wait until it finish)
  • sudo composer require slim/slim "^3.0"
  • navigate to the slim directory
  • cd slim
  • create new folder called public
  • mkdir public
  • create new file index.php inside public directory, with the following code
  • <?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;
  • create .htaccess file inside public folder:
  • RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule . index.php [L]
  • 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:
  • <VirtualHost *:80> 
    ServerName slim.local
    DocumentRoot /var/www/html/slim/public/ 
    <Directory /var/www/html/slim/public/> 
    AllowOverride all 
  • open url of the virtual host, let's say url is slim.local/ and then access slim.local/hello/lubuntu
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.

No comments:

Post a Comment