Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / PHP

Slim framework installation

4.89/5 (2 votes)
6 Aug 2014CPOL1 min read 11.4K  
How to install Slim Framework on Ubuntu?

Introduction

Slim is a tiny PHP framework that helps you quickly developing simple and powerful web applications and web services.

Features

  • Slim is very simple and easy to configure.
  • Slim is used to rendering template with custom views.
  • Slim supports flash messaging.
  • It's also stored HTTP caching.
  • Slim app should encrypt its HTTP cookies.
  • Slim supports the concept of modes.
  • Slim will use to built-in error handler if debugging is enable.

Installation

Composer Install

Simple way is to download and extract the Slim Framework into your project directory and require it in your application’s index.php file.

PHP
http://www.slimframework.com/install/

Using the code

You need to give 777 -R permissions to your project folder.

Install composer:

PHP
curl -s https://getcomposer.org/installer | php

Install via composer:

PHP
composer install
PHP
php composer.phar install

You also need to register Slim’s autoloader.

PHP
require 'Slim/Slim.php';

Make sure you required Slim.php file that belongs to in Slim folder. Otherwise you won't be able to use Slim framework.

Then invoke this static registerAutoloader() function define into the Slim class. It is necessary to registerAutoloader() function if you won't register nothing will be work.

PHP
\Slim\Slim::registerAutoloader();

The next step would be just need to instantiate the \Slim\Slim() class and use referrence as an object, use that object in our code.

PHP
$application = new \Slim\Slim();

Define a HTTP GET route:

PHP
$application->get(
'/hi/:firstname/:lastname',
function ($first,$last){
echo "Hi!! $first $last";
});

'/hi/:firstname/:lastname' is the URL. If this URL is match then following function will be invoked using GET HTTP method.

PHP
function ($first,$last){
echo "Hi!! $first $last";
});

Run the your Slim application:

PHP
$application->run();

The URL address like

http://localhost/slimsample/helloslim.php/hello/Bryan/Adams

helloslim.php is my Slim application page.

hi/Bryan/Adams append with the URL address if it would be match to hi/:firstname/:lastname and if it's available the generate the output using passing parameters $first and $last.

Output is,

PHP
Hi!! Bryan Adams

Using Slim is very simple. For Advanced knowledge, You can refer following links.

References

http://docs.slimframework.com/

http://www.slimframework.com/

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)