Install Slim 3*

 

 


Installation

It's recommended that you use Composer to install Slim.

$ composer require slim/slim "^3.0"


This will install Slim and all required dependencies. Slim requires PHP 5.5.0 or newer.
Usage

Create an index.php file with the following contents:

<?php

require 'vendor/autoload.php';

$app = new Slim\App();

$app->get('/hello/{name}', function ($request, $response, $args) {
    return $response->getBody()->write("Hello, " . $args['name']);
});

$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".


setup http://localhost:8000/hello/world to http://localhost/hello/world

 Create .htaccess file with the following contents:

# Note: see https://httpd.apache.org/docs/current/howto/htaccess.html:
#
# "You should avoid using .htaccess files completely if you have access to
# httpd main server config file. Using .htaccess files slows down your Apache
# http server. Any directive that you can include in a .htaccess file is
# better set in a Directory block, as it will have the same effect with
# better performance."

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]


Installation SLIM 4*

 


System Requirements

Web server with URL rewriting
PHP 7.4 or newer

 

composer require slim/slim:"4.*"

 Step 3: Install a PSR-7 Implementation and ServerRequest Creator

Before you can get up and running with Slim you will need to choose a PSR-7 implementation that best fits your application. In order for auto-detection to work and enable you to use AppFactory::create() and App::run() without having to manually create a ServerRequest you need to install one of the following implementations:
Slim PSR-7

composer require slim/psr7


Nyholm PSR-7 and Nyholm PSR-7 Server

composer require nyholm/psr7 nyholm/psr7-server


Guzzle PSR-7
For usage with Guzzle PSR-7 version 2:

composer require guzzlehttp/psr7 "^2"


For usage with Guzzle PSR-7 version 1:

composer require guzzlehttp/psr7 "^1"
composer require sapphirecat/slim4-http-interop-adapter

Laminas Diactoros

composer require laminas/laminas-diactoros

Step 4: Hello World

Create File: public/index.php

<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;

require __DIR__ . '/../vendor/autoload.php';

$app = AppFactory::create();

$app->get('/', function (Request $request, Response $response, $args) {
    $response->getBody()->write("Hello world!");
    return $response;
});

$app->run();

You may quickly test this using the built-in PHP server:

$ php -S localhost:8000 -t public

 Going to http://localhost:8000/hello/world 

setup  

http://localhost:8000/hello/world 

to http://localhost/hello/world

 

 composer require selective/basepath

Create File: index.php

 <?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
use Selective\BasePath\BasePathDetector;


require __DIR__ . '/vendor/autoload.php';

// Instantiate App
$app = AppFactory::create();

// Add error middleware
$app->addErrorMiddleware(true, true, true);
// Set the base path to run the app in a subdirectory.
// This path is used in urlFor().
$basePath = (new BasePathDetector($_SERVER))->getBasePath();

$app->setBasePath($basePath.'/slim4');




// Add routes
$app->get('/', function (Request $request, Response $response) {
    $response->getBody()->write('<a href="./hello/world">Try /hello/world</a>');
    return $response;
});

$app->get('/hello/{name}', function (Request $request, Response $response, $args) {
    $name = $args['name'];
    $response->getBody()->write("Hello, $name");
    return $response;
});




$app->run();

Add the following line to .htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /slim4/index.php [QSA,L]

done

source : https://github.com/slimphp/Slim/issues/2512

 
 https://github.com/slimphp/Slim

https://github.com/selective-php/basepath#installation