Slim Framework: Difference between revisions
Jump to navigation
Jump to search
(28 intermediate revisions by the same user not shown) | |||
Line 5: | Line 5: | ||
== Installation == | == Installation == | ||
Core | === Core === | ||
composer require slim/slim:"4.*" | composer require slim/slim:"4.*" | ||
composer require slim/psr7 | composer require slim/psr7 | ||
Additional libraries | === Additional libraries === | ||
composer require filp/whoops | composer require filp/whoops | ||
composer require twig/twig | composer require twig/twig | ||
composer require twig/intl-extra | composer require twig/intl-extra | ||
composer require slim/twig-view | |||
composer require doctrine/orm | composer require doctrine/orm | ||
composer require vlucas/phpdotenv | composer require vlucas/phpdotenv | ||
Line 20: | Line 21: | ||
composer require phpunit/phpunit --dev | composer require phpunit/phpunit --dev | ||
Directories | === Directories === | ||
mkdir public | mkdir public | ||
mkdir src | |||
mkdir templates | |||
mkdir tests | |||
<code>.gitignore</code> | === <code>.gitignore</code> === | ||
vendor/ | vendor/ | ||
=== <code>composer.json</code> autoloader === | |||
"autoload": { | |||
"psr-4": { | |||
"App\\": "src/" | |||
} | |||
}, | |||
"autoload-dev": { | |||
"psr-4": { | |||
"App\\Test\\": "tests/" | |||
} | |||
} | |||
Run <code>composer dump-autoload</code> to generate the autoloader. | |||
=== <code>.env</code> file === | |||
APP_DEFAULT_TIMEZONE="Europe/London" | |||
=== <code>templates/base.twig.html</code> === | |||
<!doctype html> | |||
<html lang="en"> | |||
<head> | |||
<meta charset="utf-8"> | |||
<meta name="viewport" content="width=device-width, initial-scale=1"> | |||
<title><nowiki>{{</nowiki> block('title') <nowiki>}}</nowiki></title> | |||
<link rel="stylesheet" href="/css/pure-min.css"> | |||
</head> | |||
<body> | |||
<nowiki>{{</nowiki> block('content') <nowiki>}}</nowiki> | |||
</body> | |||
</html> | |||
=== <code>templates/index.twig.html</code> === | |||
{% extends 'base.twig.html' %} | |||
{% block content %} | |||
<nowiki><p>Hello {{ name }}</p></nowiki> | |||
{% endblock %} | |||
=== <code>public/index.php</code> === | |||
<?php | |||
declare(strict_types = 1); | |||
use Psr\Http\Message\ResponseInterface; | |||
use Psr\Http\Message\ServerRequestInterface; | |||
use DI\ContainerBuilder; | |||
use Slim\Factory\AppFactory; | |||
use Slim\Views\Twig; | |||
use Slim\Views\TwigMiddleware; | |||
require_once __DIR__ . '/../vendor/autoload.php'; | |||
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__); | |||
$dotenv->load(); | |||
date_default_timezone_set($_ENV['APP_DEFAULT_TIMEZONE']); | |||
$containerBuilder = new Container(); | |||
AppFactory::setContainer($container); | |||
$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 | |||
== Adopting in a legacy project == | |||
How to add Slim to an existing project which doesn't use a framework. Slim is probably the only actively maintained framework where this is possible, as Laravel and Symfony both expect you to use them exclusively. | |||
== Links == | == Links == |
Latest revision as of 07:27, 5 May 2022
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 slim/twig-view 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 mkdir templates mkdir tests
.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_DEFAULT_TIMEZONE="Europe/London"
templates/base.twig.html
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>{{ block('title') }}</title> <link rel="stylesheet" href="/css/pure-min.css"> </head> <body> {{ block('content') }} </body> </html>
templates/index.twig.html
{% extends 'base.twig.html' %} {% block content %} <p>Hello {{ name }}</p> {% endblock %}
public/index.php
<?php declare(strict_types = 1); use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use DI\ContainerBuilder; use Slim\Factory\AppFactory; use Slim\Views\Twig; use Slim\Views\TwigMiddleware; require_once __DIR__ . '/../vendor/autoload.php'; $dotenv = Dotenv\Dotenv::createImmutable(__DIR__); $dotenv->load(); date_default_timezone_set($_ENV['APP_DEFAULT_TIMEZONE']); $containerBuilder = new Container(); AppFactory::setContainer($container); $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
Adopting in a legacy project
How to add Slim to an existing project which doesn't use a framework. Slim is probably the only actively maintained framework where this is possible, as Laravel and Symfony both expect you to use them exclusively.