Last updated on February 20, 2018
Slim is a PHP micro framework that helps you write simple web applications and APIs quickly. It comes with a sophisticated URL dispatcher and middleware architecture that makes it ideal for static websites or API prototyping. It supports all(GET, POST, PUT, DELETE) the HTTP methods. Slim requires PHP 5.5.0 or newer.
This tutorial shows how to install Slim Framework. The slim framework is extremely easy to setup, thanks to composer support.
How to install Slim Framework 3
Slim Framework 3 – Route Groups
Slim Framework 3 – configure/load database in Slim Framework 3
Installation
This guide explains you downloading slim framework using composer.
# change into your projects directory $ cd xampp\htdocs # create project directory $ mkdir slimapp #change into the App directory $ cd slimapp # install slim via composer $ composer require slim/slim "^3.0"
Above issued composer command will install Slim and all required dependencies. Now, lets use the slim framework and say hello to the world.
Now just create index.php file containing:
get('/hello/{name}', function ($request, $response, $args) { $response->write("Hello, " . $args['name']); return $response; }); $app->run();
You may quickly test this using the built-in PHP server:
$ php -S localhost:8000
Going to http://localhost:8000/hello/world
will now display “Hello, world”.
Without using built-in php server,you can access application if your files are hosting in server, with
.
http://localhost/slim/index.php/hello/world
If you want to remove index.php from the URL, then change the apache configurations with .htaccess
. The .htaccess
file should contain this code:
RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^ index.php [QSA,L]
Please ensure your .htaccess
and index.php
files are in the same public-accessible directory.
Make sure your Apache virtual host is configured with the AllowOverride option so that the .htaccess rewrite rules can be used – AllowOverride All