What are the steps to install Slim Framework via Composer?

What are the steps to install Slim Framework via Composer?

Here are the steps to install Slim Framework via Composer:

Step 1: Install Composer

Ensure Composer is installed on your machine. If not, download and install it from getcomposer.org.

Step 2: Create a New Project Directory

Create a directory for your Slim project.

Example

mkdir my-slim-app
cd my-slim-app

Step 3: Install Slim Framework

Run the following Composer command to install Slim Framework:

Example

composer require slim/slim:"^4.0"

Step 4: Install PSR-7 and Middleware (Required)

Slim requires PSR-7 and a middleware dispatcher. Install the required packages:

Example

composer require nyholm/psr7
composer require slim/psr7

Step 5: Create an Entry Point (index.php)

Create an index.php file in your project folder to handle requests:

Example

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

$app = \Slim\Factory\AppFactory::create();
$app->get('/', function ($request, $response, $args) {
    $response->getBody()->write("Hello, Slim!");
    return $response;
});
$app->run();
?>

Step 6: Run the Application

Start the PHP built-in server to run your Slim app:

Example

php -S localhost:8080 -t .

Visit http://localhost:8080 in your browser, and you’ll see “Hello, Slim!”

Related Questions & Topics