Slim Framework: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
|||
Line 47: | Line 47: | ||
APP_TIMEZONE="Europe/London" | APP_TIMEZONE="Europe/London" | ||
=== <code>public/index.php</code> === | |||
<?php | |||
declare(strict_types = 1); | |||
use Psr\Http\Message\ResponseInterface; | |||
use Psr\Http\Message\ServerRequestInterface; | |||
use Slim\Factory\AppFactory; | |||
require_once __DIR__ . '/../vendor/autoload.php'; | |||
$app = AppFactory::create(); | |||
$app->addRoutingMiddleware(); | |||
$app->addBodyParsingMiddleware(); | |||
$app->addErrorMiddleware(true, true, true); | |||
$app->get('/hello/[{name}]', function( | |||
ServerRequestInterface $request, | |||
ResponseInterface $response, | |||
array $args | |||
): ResponseInterface { | |||
$name = $args['name'] ?? 'unknown'; | |||
$response->getBody()->write("hello $name"); | |||
return $response; | |||
}); | |||
$app->run(); | |||
== Slim Skeleton == | == Slim Skeleton == |
Revision as of 15:21, 30 October 2021
Requirements
Slim 4 requires PHP 7.2 or greater. The current Ubuntu LTS and Debian stable include 7.4, so this requirement is satisfied on up to date servers.
Installation
Core
composer require slim/slim:"4.*" composer require slim/psr7
Additional libraries
composer require filp/whoops composer require twig/twig composer require twig/intl-extra composer require doctrine/orm composer require vlucas/phpdotenv composer require php-di/php-di --with-all-dependencies composer require phpunit/phpunit --dev
Directories
mkdir public mkdir src
.gitignore
vendor/
composer.json
autoloader
"autoload": { "psr-4": { "App\\": "src/" } }, "autoload-dev": { "psr-4": { "App\\Test\\": "tests/" } }
Run composer dump-autoload
to generate the autoloader.
.env
file
APP_TIMEZONE="Europe/London"
public/index.php
<?php declare(strict_types = 1);
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Slim\Factory\AppFactory; require_once __DIR__ . '/../vendor/autoload.php'; $app = AppFactory::create(); $app->addRoutingMiddleware(); $app->addBodyParsingMiddleware(); $app->addErrorMiddleware(true, true, true); $app->get('/hello/[{name}]', function( ServerRequestInterface $request, ResponseInterface $response, array $args ): ResponseInterface { $name = $args['name'] ?? 'unknown'; $response->getBody()->write("hello $name"); return $response; }); $app->run();
Slim Skeleton
composer create-project slim/slim-skeleton my-app