phil / apisymfony
Start a REST API on Symfony and API Platform, with API keys, problem details, a checked OpenAPI document and every quality gate already passing.
Package info
github.com/parler-haut-interagir-librement/ApiSymfony
Type:project
pkg:composer/phil/apisymfony
Requires
- php: >=8.5
- ext-ctype: *
- ext-dom: *
- ext-iconv: *
- ext-libxml: *
- ext-mbstring: *
- ext-simplexml: *
- ext-xml: *
- api-platform/doctrine-orm: ^5.0
- api-platform/metadata: ^5.0
- api-platform/openapi: ^5.0
- api-platform/symfony: ^5.0
- doctrine/dbal: ^4.6
- doctrine/doctrine-bundle: ^3.4
- doctrine/doctrine-migrations-bundle: ^4.0
- doctrine/migrations: ^3.10
- doctrine/orm: ^3.8
- doctrine/persistence: ^4.2
- league/commonmark: ^2.10
- phil/api-contract: ^0.1@alpha
- phil/api-contract-bundle: ^0.3@alpha
- phil/api-keys-bundle: ^0.2@alpha
- phil/cors-bundle: ^0.1.0@alpha
- phil/idempotency: ^0.3@alpha
- sentry/sentry-symfony: ^5.17
- symfony/asset: ~8.2.0
- symfony/asset-mapper: ~8.2.0
- symfony/cache-contracts: ^3.8
- symfony/console: ~8.2.0
- symfony/doctrine-bridge: ~8.2.0
- symfony/dotenv: ~8.2.0
- symfony/expression-language: ~8.2.0
- symfony/filesystem: ~8.2.0
- symfony/flex: ^2.12
- symfony/framework-bundle: ~8.2.0
- symfony/http-foundation: ~8.2.0
- symfony/http-kernel: ~8.2.0
- symfony/intl: ~8.2.0
- symfony/monolog-bundle: ^4.1
- symfony/property-access: ~8.2.0
- symfony/property-info: ~8.2.0
- symfony/routing: ~8.2.0
- symfony/runtime: ~8.2.0
- symfony/security-bundle: ~8.2.0
- symfony/serializer: ~8.2.0
- symfony/stimulus-bundle: ^3.8
- symfony/string: ~8.2.0
- symfony/translation: ~8.2.0
- symfony/twig-bundle: ~8.2.0
- symfony/uid: ~8.2.0
- symfony/ux-turbo: ^3.8
- symfony/validator: ~8.2.0
- symfony/web-link: ~8.2.0
- symfony/yaml: ~8.2.0
- twig/extra-bundle: ^3.31
- twig/markdown-extra: ^3.31
- twig/twig: ^3.31
Requires (Dev)
- api-platform/test: ^5.0
- bamarni/composer-bin-plugin: ^1.9
- brianium/paratest: ^7.30
- ergebnis/phpunit-slow-test-detector: ^2.29
- phil/dev-tools: ^0.2.1@alpha
- phpunit/phpunit: ^13.4
- shipmonk/composer-dependency-analyser: ^1.8
- symfony/browser-kit: ~8.2.0
- symfony/css-selector: ~8.2.0
- symfony/debug-bundle: ~8.2.0
- symfony/http-client: ~8.2.0
- symfony/maker-bundle: ^1.68
- symfony/requirements-checker: ^2.0
- symfony/stopwatch: ~8.2.0
- symfony/web-profiler-bundle: ~8.2.0
- zenstruck/foundry: ^2.17
Suggests
None
Provides
None
Conflicts
README
About 🖋
ApiSymfony is a Symfony 8.2 application template for a REST API, ready to use: an API Platform application with an example resource, API tokens with scopes, errors as problem details, an OpenAPI document checked against the contract of an unversioned API, tests of every kind and strict quality gates. Start an API from it, delete the example, and keep the rest.
It derives from MicroSymfony, the PHIL version of strangebuzz/MicroSymfony, created and maintained by COil of Strangebuzz: the same tasks, the same tests, the same gates and the same identity, with API Platform and a database instead of the examples of pages. What differs, and why, is listed in Differences from MicroSymfony.
Table of Contents 📖
- Demo
- Quick-start
- Requirements
- Stack
- The API
- Tasks
- Quality gates
- Differences from MicroSymfony
- Notes
- What it doesn't ship?
- PHIL bundles
- References
- Others “Proclaim Honestly, Interact Liberally” Skeletons
- Contributing
- Security
- Credits
- License
- Built with ApiSymfony
Demo 🌈
A demo of ApiSymfony will run in the browser on ph-il.ca, with the other templates of PHIL.
Quick-start ⚡
Create a new project from the latest release:
composer create-project phil/apisymfony my-api && cd my-api
The repository is also a GitHub template repository: the "Use this template" button creates a new repository from the main branch, whose commits all pass the continuous integration.
With the Symfony binary 🎶
You need the Symfony binary and
Castor. The database is a SQLite file in var/, migrated
before the server starts:
castor start
Open https://127.0.0.1:8000/api/docs (if port 8000 is free) for the Swagger UI of
the API, or https://127.0.0.1:8000 for this README. castor stop stops the server.
With FrankenPHP and PostgreSQL 🧟
You need Docker. compose.yaml serves the application with
FrankenPHP in worker mode, which boots the kernel once and serves request after request,
and restarts its workers when a file changes; the database is PostgreSQL 17, migrated once the services are up:
castor up
Open https://localhost/api/docs. castor down stops the services. The image of
docker/Dockerfile is for development: an API deployed from this template brings its own deployment.
With MySQL 🐬
MySQL is the other database the template supports. In compose.yaml, replace the database service with a
mysql:8.4 image and set DATABASE_URL to:
mysql://app:app@database:3306/app?serverVersion=8.4&charset=utf8mb4
With the Symfony binary, set the same variable in .env.local, with the host of your server. The migration describes
its tables through the schema API of Doctrine DBAL and is written in the SQL of the database it runs on: it runs on
SQLite, PostgreSQL and MySQL without a change.
Requirements ⚙
- PHP 8.5 or later, with the extensions of
composer.json, and Castor - The Symfony binary, or Docker
- The Xdebug or PCOV extension for the code coverage report
Stack 🔗
- PHP 8.5
- Symfony 8.2, from its release candidate, with Symfony Security
- API Platform 5, REST only
- Doctrine ORM 3 (DBAL 4) and its migrations, on SQLite, PostgreSQL or MySQL
phil/api-keys-bundleandphil/api-contract-bundlephil/cors-bundlefor the cross-origin requests- Twig 3 and Pico CSS 2 for the home page, in English and in French
- Sentry for the errors of the production environment
- PHPUnit 13 and Foundry
- FrankenPHP in worker mode, through Docker Compose
The API 🔌
The resources
API Platform turns each class marked #[ApiResource] into REST operations under /api. The example is
Book, a Doctrine entity:
| Operation | Who |
|---|---|
GET /api/books, GET /api/books/{id} |
Everyone |
POST /api/books, PATCH /api/books/{id}, DELETE /api/books/{id} |
A token with the scope books:write |
The resources are written in JSON-LD (application/ld+json) or in JSON (application/json), as the Accept header
asks; a change is sent as application/merge-patch+json. The collections are paginated, 30 items a page. API
Platform is configured for REST only: no GraphQL. The configuration is in
config/packages/api_platform.yaml.
The tokens
The API is stateless: a request carries a token of phil/api-keys-bundle
in its Authorization header, checked by the access_token authenticator of Symfony. A token is shown once, when it
is created; the database keeps only its keyed hash. It holds scopes, expires, and can be revoked or rotated:
castor token
curl -X POST https://127.0.0.1:8000/api/books \
-H "Authorization: Bearer apisf_…" -H "Content-Type: application/ld+json" \
-d '{"title": "Kamouraska", "author": "Anne Hébert"}'
castor token creates a token for example-client, the client config/packages/security.yaml
keeps in memory, with the scope books:write. The scopes are declared in
config/packages/phil_api_keys.yaml, and each operation that writes names the one
it requires in its security expression: is_granted('API_SCOPE', 'books:write'). bin/console phil:api-keys:list,
phil:api-keys:revoke and phil:api-keys:purge-expired manage the tokens.
The tokens are hashed with API_KEYS_HASH_KEY, 32 random bytes or more. .env.dev and .env.test hold keys known to
everyone, for the workstation and the tests; .env leaves it empty, so that no other environment starts with a known
key. Set it as a secret:
php -r 'echo bin2hex(random_bytes(32)), PHP_EOL;' | bin/console secrets:set API_KEYS_HASH_KEY -
The errors
Every error of the API is a problem (RFC 9457), application/problem+json: API Platform writes those of its
operations, with the violations of the constraints for a 422; phil/api-contract-bundle writes the 401 of a
missing or refused token, with its WWW-Authenticate challenge, and every ProblemException a controller throws; a
403 names no reason. Symfony writes the others as problems too for a client that accepts JSON, such as the 404 of
SlugifyAction, an endpoint beside the resources.
The OpenAPI document
API Platform publishes the OpenAPI document of the API at /api/docs.jsonopenapi, and its Swagger UI at /api/docs,
with an "Authorize" button for the bearer token. castor lint:openapi exports it to var/openapi.json and checks it
against the rules of phil/api-contract for an unversioned REST
and JSON API: no version in a path, JSON media types, problem details for the errors, and more. It fails on a warning;
OpenApiContractTest runs the same check with the tests.
Cross-origin requests
phil/cors-bundle answers the cross-origin requests on /api, as
config/packages/phil_cors.yaml sets them:
- the origins that
CORS_ALLOW_ORIGINmatches, a regular expression anchored by^and$:localhostand127.0.0.1, on any port, in.env; set the origins of your front ends in.env.localor in the environment of the server; - the methods
GET,POST,PATCHandDELETE, the request headersContent-TypeandAuthorization, the response headerLinkreadable by the page, preflights kept an hour by the browser; - no credentials: the API reads a bearer token, never a cookie, so no origin may send one.
The bundle answers the preflights before the router and the firewall, so no OPTIONS route is needed, and adds
Vary: Origin to every answer of /api. Another origin gets no Access-Control-Allow-Origin, which the browser reads
as a refusal; the pages outside /api send no CORS header. A wildcard origin with credentials, an unanchored pattern
or the origin null stops the application on its first request rather than open the API.
CorsTest checks these rules.
Tasks 🧰
castor lists every task. The tests, the lints, the fixers, ci and hooks are the common tasks of
phil/dev-tools, which castor.php imports from
vendor/phil/dev-tools/resources/castor/tasks.php and sets through its PHIL_CASTOR constant: the suites, the
coverage threshold, and the checks of this application that lint and ci call after theirs (lint:doctrine,
lint:openapi, test:schema). castor.php keeps the tasks of this application only. The main ones:
| Task | What it does |
|---|---|
castor start, castor stop |
The Symfony binary's web server, on SQLite. |
castor up, castor down |
FrankenPHP in worker mode and PostgreSQL, with Docker Compose. |
castor migrate |
The migrations, on the database of DATABASE_URL. |
castor token |
An API token for the example client, with the scope books:write. |
castor test |
Every PHPUnit suite; --filter and --options pass on to PHPUnit. |
castor test:unit, test:integration, test:api, test:functional, test:e2e |
One suite; castor test:suite <name> runs any suite of phpunit.dist.xml. |
castor test:next |
The tests on the next PHP version, in its Docker image. |
castor test:schema |
The migrations on a fresh SQLite database, then its schema compared with the Doctrine mapping. |
castor coverage |
The HTML coverage report in var/coverage/, and its threshold. |
castor infection |
Mutation testing. |
castor taint |
The taint analysis of Psalm. |
castor lint |
Every lint, the Doctrine mapping, the OpenAPI contract and the static analysis. |
castor fix |
Rector, PHP-CS-Fixer, Twig-CS-Fixer, Biome and composer-normalize. |
castor ci |
The continuous integration, locally: coverage, every lint, then test:schema. |
castor hooks |
The git hooks: the fast checks on the staged files before each commit. |
Quality gates 🚦
The checks come from phil/dev-tools, the shared tooling of PHIL,
adapted to an application. Each tool is installed in its own vendor-bin/<tool>/ directory by the
Composer bin plugin, so that its dependencies never meet the
application's. The thin configuration files at the root (phpstan.neon, rector.php, .php-cs-fixer.dist.php,
arkitect.php) call the shared ones; phil-dev.php holds what is particular to the application, each exception with
its reason.
| Task | Checks |
|---|---|
castor lint:composer |
The dependency analysis (a class used without its package required), composer audit, composer-unused and composer-normalize. |
castor lint:php |
PHP-CS-Fixer: @Symfony, @Symfony:risky, strict types and strict comparisons. |
castor lint:twig |
lint:twig, then the code style of the templates. |
castor lint:js |
Biome on the Stimulus controllers. |
castor lint:yaml, lint:container |
The YAML files, config/ and the messages included, and the container. |
castor lint:doctrine |
The Doctrine mapping. |
castor lint:openapi |
The OpenAPI document against the contract of an unversioned REST and JSON API. |
castor stan (lint:analyse) |
PHPStan at the maximum level, with the Doctrine and Symfony extensions, type coverage at 100 %, a cognitive complexity limit and forbidden calls. |
castor lint:rector |
Rector has nothing left to change. |
castor lint:architecture |
Arkitect on the shape of the classes; Deptrac on the libraries src/ may use, listed in phil-dev.php. |
The functional suite holds the route smoke test of phil/dev-tools (RouteSmokeTesting): every GET route that needs
no parameter answers without a server error, the home page, /api/slugify, /api/docs, the entry point of the API and
the GET collection of each resource included.
The taint analysis of Psalm, castor taint, runs on its own, before a release and once a week. The .github/
directory also holds the security files of phil/dev-tools: Dependabot, with a cooldown of seven days, the OpenSSF
Scorecard and zizmor, which audits the workflows.
Differences from MicroSymfony 🔀
| MicroSymfony | This template | Why |
|---|---|---|
| No database | Doctrine ORM and its migrations: SQLite for the Symfony binary, PostgreSQL in compose.yaml, MySQL documented |
An API keeps its resources and its tokens. |
| A JSON endpoint in a controller | API Platform 5, REST only, beside one plain controller | The operations, the pagination, the content negotiation, the validation and the OpenAPI document come from the resource. |
| No security | A stateless firewall on /api, API tokens with scopes |
A public API knows who calls and what for. |
| Errors as HTML pages | Problem details (RFC 9457) under /api |
A client reads an error as it reads a resource. |
| The form, Stimulus and Composer examples | The Book resource |
The example shows what an API needs. |
symfony/form, symfony/security-csrf |
None | A stateless API has no form and no session to protect. |
| No API contract | The OpenAPI document checked by castor lint:openapi and a test |
The API stays as its document describes it. |
The rest stays: Symfony 8.2 from its release candidate, PHP 8.5, Castor only, the gates of phil/dev-tools, the five
test suites, the code coverage at 100 %, FrankenPHP in worker mode, Sentry in production, English and French pages, the
PHIL identity and the credit to Strangebuzz.
Notes 📒
Adding a resource
Create an entity with bin/console make:entity --api-resource, or a class of src/ApiResource/ with a state provider
and processor for what Doctrine does not keep. Give each operation that writes a security expression on a scope,
declare that scope in config/packages/phil_api_keys.yaml, write the migration through the schema API, as
the first one does, and add its tests under tests/Api/.
Accounts
The owners of the tokens are the clients of a user provider in memory. An API with accounts replaces it with its own
provider, an entity provider for instance, and the members create their tokens on the page of
phil/api-keys-bundle; the bundle also declares other owner types, such as organisations or applications.
Idempotency and deprecations
phil/api-contract-bundle keeps the idempotency keys in the table phil_idempotency: a controller marked
#[Idempotent] runs once per Idempotency-Key and replays its response. A controller marked #[DeprecatedEndpoint]
announces its Deprecation and Sunset headers.
YAML configuration files
The configuration of Symfony, of API Platform and of the bundles is in YAML, under config/:
config/packages/*.yaml, with when@dev, when@test and when@prod blocks for what changes by environment,
config/routes.yaml, config/routes/*.yaml and config/services.yaml. A Flex recipe adds its files in the same
format. Only config/bundles.php and config/preload.php stay PHP. castor lint:yaml checks every file of
config/, and castor lint:container checks the container they build.
# config/packages/phil_api_keys.yaml phil_api_keys: scopes: 'books:write': description: 'Create, change and delete the books'
Languages
The home page is in English, the default locale, and in French, chosen from the browser's Accept-Language header on
the same address. The messages are in translations/.
Sentry
In the production environment, the errors go to Sentry once SENTRY_DSN holds the DSN of your
project, set as a real environment variable or a secret, never in a committed file. While it is empty, nothing is sent.
What it doesn't ship? ❌
- GraphQL: the API is REST only
- Accounts, an administration and pages beyond the home page: start from
WebSymfony (
phil/websymfony) or WebSkeleton (phil/webskeleton) - A deployment: each API brings its own
PHIL bundles 🧩
The bundles of PHIL add what an API needs, one at a time, with composer require. A few that suit
an API:
phil/rate-limit-bundleandphil/usage-meter-bundle: rate limits and quotas by token, with theRateLimitheadersphil/ip-anonymizer: the address a token was last used from, anonymizedphil/browser-security-bundle: the security headers of the responsesphil/well-known-bundle:security.txtand the other files of/.well-known/
The PHIL organization on GitHub lists every package.
References 📚
- API Platform: the documentation (api-platform.com)
- Testing the API (api-platform.com)
- RFC 9457: Problem Details for HTTP APIs (rfc-editor.org)
- RFC 6750: Bearer Token Usage (rfc-editor.org)
- Introducing the MicroSymfony application template (strangebuzz.com)
- Organizing your Symfony project tests (strangebuzz.com)
- Castor, a journey across the sea of task runners (jolicode.com)
- The Symfony Framework Best Practices (symfony.com)
Others “Proclaim Honestly, Interact Liberally” Skeletons 🩻
- MicroSymfony (
phil/microsymfony): the micro template ApiSymfony derives from, with no database, Pico CSS, Stimulus and Turbo, Castor, the tests and quality gates. - WebSymfony (
phil/websymfony): MicroSymfony with Doctrine, accounts, EasyAdmin, KnpMenu, NelmioSecurityBundle and NelmioCorsBundle, still with Pico CSS. - WebSkeleton (
phil/webskeleton): WebSymfony and the PHIL tools, organised in DDD (Domain, Application, Infrastructure), in French and in English (/fr,/en).
Contributing 🤝
See CONTRIBUTING and the Code of Conduct.
Security 🧯
See SECURITY.
Credits 🙏
- Philippe Gamache, maintainer, for Parler Haut, Interagir Librement
- COil, author of strangebuzz/MicroSymfony, and its contributors
- All the contributors of ApiSymfony
License ⚖️
The MIT License, with the attribution to the original repository it asks for. See the license file.
Built with ApiSymfony 🛠️
Nothing listed yet: an API built with ApiSymfony is welcome here, by a pull request.