How to manually install Slim framework

I’ve received a few requests regarding manual installation of Slim Framework. Although I prefer using Composer, many people have issues when installing on remote hosts. The following post provides a short description on manual installation of Slim framework.

1. First download the Slim zip archive from github – https://github.com/slimphp/Slim

2. Extract from the archive and copy the Slim directory to your public_html directory or wherever you need to install. Note that there are a couple of nested sub-directories named slim. Copy the one that has the ‘Slim.php’ file in it.

3. Copy the following test code to a file named hello.php

<?php
 
require 'Slim/Slim.php';
 
\Slim\Slim::registerAutoloader();
 
$app = new \Slim\Slim();
$app->get('/hello/:name', function ($name) {
    echo "Hello, $name! Welcome to Slim Framework";
});
 
$app->run();
 
?>

4. In your browser navigate to the URL http://www.example.com/hello.php/hello/world, where example.com represents your domain name, this could even be your localhost. You should see ‘Hello, world! Welcome to Slim Framework’ in the browser.

Note that the example assumes that you uploaded the Slim Framework to the public_html directory, which contains the Slim subdirectory. If you uploaded the Slim Framework to some other directory make sure that you change the ‘require’ statement accordingly. For example if you installed it in ‘frameworks’ subdirectory change the ‘require’ to the following.

require ‘frameworks/Slim/Slim.php';

The main thing to take notice is of Slim::registerAutoloader(), which handles all the autoloading of classes. When you instead use Composer to install the framework the ‘autoload.php’ file installed by Composer takes care of the autoloading.

You can add the PHP use keyword to change the namespace of the code. This will allow you to import the ‘Slim/Slim’ namespace in the current code context and hence drop the namespace prefixes before method names.

<?php
 
require 'Slim/Slim.php';
 
use \Slim\Slim;
 
Slim::registerAutoloader();
 
$app = new Slim();
$app->get('/hello/:name', function ($name) {
    echo "Hello, $name! Welcome to Slim Framework";
});
 
$app->run();
 
?>

Hope this helps in performing a manual installation of Slim framework.



2 thoughts on “How to manually install Slim framework

Comments are closed.