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