search
  • Sign In
  • Sign Up
Password reset successful

Follow the proiects vou are interested in andi aet the latestnews about them taster

Table of Contents
Install Slim Using Composer
Create a Basic Application File
Configure Web Server or Use Built-in PHP Server
Additional Setup Tips
Home Development Tools composer How to set up a project with the Slim Framework using Composer

How to set up a project with the Slim Framework using Composer

Nov 28, 2025 am 12:05 AM
composer

Install PHP 8.1 and Composer, then run composer create-project slim/slim-skeleton my-app for a full setup or composer require slim/slim:"^4.0" for minimal. 2. Create a public/ directory and add index.php with route definition using AppFactory. 3. Use php -S localhost:8080 -t public to run locally. 4. For production, configure Apache/Nginx to point to public/. 5. Enhance with middleware, DI container, organized routes, and .env via vlucas/phpdotenv.

How to set up a project with the Slim Framework using Composer

To set up a project with the Slim Framework using Composer, follow these clear steps. Slim is a lightweight PHP micro-framework that helps you build web applications and APIs quickly. Composer is the standard dependency manager for PHP, making it easy to install and manage Slim and its components.

Install Slim Using Composer

Make sure you have PHP (version 8.1 or higher recommended) and Composer installed on your system. Then run the following command in your terminal to create a new project directory and install Slim:

composer create-project slim/slim-skeleton my-app

This command sets up a full-featured starter project using the official Slim Skeleton. If you prefer a minimal setup instead, navigate to your project folder and require Slim directly:

mkdir my-app && cd my-app
composer require slim/slim:"^4.0"

Create a Basic Application File

Create an index.php file in a public-facing directory (recommended: public/). First, create the folder structure:

mkdir public
touch public/index.php

Add this basic code to public/index.php to define a simple route:


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) {
$response->getBody()->write('Hello, Slim!');
return $response;
});

$app->run();

Configure Web Server or Use Built-in PHP Server

For local development, use PHP’s built-in server. Run this command from the root of your project:

php -S localhost:8080 -t public

Visit http://localhost:8080 in your browser. You should see “Hello, Slim!”.

If deploying to production, configure your web server (Apache or Nginx) to point to the public/ directory and route all requests to public/index.php.

Additional Setup Tips

Consider these common next steps to enhance your Slim project:

  • Install middleware for routing, error handling, or CORS: composer require slim/psr7
  • Add a dependency injection container if needed (Slim supports PHP-DI out of the box)
  • Organize routes into separate files for larger apps
  • Use environment variables with a .env file via vlucas/phpdotenv

Basically just install Slim via Composer, write a minimal app entry point, and serve it through the public directory. The framework stays out of your way while giving you solid tools for routing and middleware.

The above is the detailed content of How to set up a project with the Slim Framework using Composer. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

ArtGPT

ArtGPT

AI image generator for creative art from text prompts.

Stock Market GPT

Stock Market GPT

AI powered investment research for smarter decisions

Popular tool

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to generate QR code for PHP dynamic website development_PHP dynamic website QR code generation method [Tutorial] How to generate QR code for PHP dynamic website development_PHP dynamic website QR code generation method [Tutorial] Feb 04, 2026 am 10:48 AM

The most trouble-free PHP solution for dynamically generating QR codes is to use endroid/qr-code: it supports PHP7.4, PNG/SVG output, logo addition, color adjustment, and fault tolerance. It does not rely on GD (fallback compatible). Chinese requires UTF-8 encoding with high error correction or urlencode. WriteString() can be used in web pages to directly output image streams.

How to install dev development dependencies in Composer_Composer --dev parameter usage [Instructions] How to install dev development dependencies in Composer_Composer --dev parameter usage [Instructions] Feb 05, 2026 pm 11:57 PM

--dev must be added, otherwise the package will be written to the require field, resulting in missing online --no-dev; --dev is written to require-dev, which is installed by default, and --no-dev is completely ignored; temporary enablement requires composerinstall --dev and the dependency exists in composer.json.

How to install ThinkPHP with composer_Steps to deploy TP framework using composer How to install ThinkPHP with composer_Steps to deploy TP framework using composer Feb 12, 2026 am 06:27 AM

The ThinkPHP stable version should be clearly specified, such as using composercreate-projecttopthink/thinktp6^6.3 to install the TP6.3LTS version to avoid pulling non-production-ready beta versions because dev-master has switched to the TP8 preview version.

How to install and configure the environment for Laravel_Steps to quickly build a development environment for Laravel [Basics] How to install and configure the environment for Laravel_Steps to quickly build a development environment for Laravel [Basics] Feb 06, 2026 am 06:45 AM

The main reason why the Laravel project fails to start is that the PHP version/extension is not up to standard, the Web server configuration is incorrect, or the .env does not take effect; it is necessary to confirm that PHP ≥ 8.1 and enable extensions such as mbstring, DocumentRoot points to the public directory, and execute key:generate and config:clear.

How to clear the Composer cache to solve installation issues How to clear the Composer cache to solve installation issues Jan 07, 2026 am 01:12 AM

Use composerclear-cache to solve problems such as installation failure and version errors caused by cache expiration or damage. This command will clear all cached files including download packages, metadata and compressed files, and then rerun composerinstall or update to obtain the latest dependency data.

Composer reports error recursion limit_Solution to Composer dependency level is too deep [Plan] Composer reports error recursion limit_Solution to Composer dependency level is too deep [Plan] Feb 05, 2026 pm 11:06 PM

The "recursionlimitexceeded" error reported by Composer is a protection mechanism triggered by its dependency parser to prevent infinite recursion. The main reason is that there are circular references or too deep nesting in the dependency graph, which is common in require-dev loose constraints, path warehouse implicit cycles, and dev branch version testing.

Composer prompts that you need to enter Token_Solve Composer GitHub authorization problem [Avoid pits] Composer prompts that you need to enter Token_Solve Composer GitHub authorization problem [Avoid pits] Feb 05, 2026 pm 11:18 PM

Composer requires the input of GitHubToken because GitHub will abandon password authentication starting in August 2021. Unauthenticated access to the API will trigger current limiting or denial. PersonalAccessToken needs to be configured and set through composerconfig-ggithub-oauth.github.com.

What is the use of Composer lock file_Analysis of the difference between Composer.lock and json [Popular Science] What is the use of Composer lock file_Analysis of the difference between Composer.lock and json [Popular Science] Feb 05, 2026 pm 11:36 PM

composer.lock is an accurate snapshot of dependencies and must be submitted to Git; it records the exact version, hash value and complete dependency tree of each package, ensuring that composerinstall installs exactly the same dependencies in all environments, avoiding compatibility issues caused by version fluctuations.

Related articles