Skip to content

Commit d44661b

Browse files
authored
docs: add Symfony-specific page (#2307)
Docs for the Symfony integration, as for Laravel and WordPress. --------- Signed-off-by: Kévin Dunglas <kevin@dunglas.fr>
1 parent 5ebf119 commit d44661b

File tree

5 files changed

+238
-69
lines changed

5 files changed

+238
-69
lines changed

CONTRIBUTING.md

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -90,60 +90,60 @@ curl -v http://127.0.0.1:8080/phpinfo.php
9090

9191
1. Configure Git to always use `lf` line endings
9292

93-
```powershell
94-
git config --global core.autocrlf false
95-
git config --global core.eol lf
96-
```
93+
```powershell
94+
git config --global core.autocrlf false
95+
git config --global core.eol lf
96+
```
9797

9898
2. Install Visual Studio, Git, and Go:
9999

100-
```powershell
101-
winget install -e --id Microsoft.VisualStudio.2022.Community --override "--passive --wait --add Microsoft.VisualStudio.Workload.NativeDesktop --add Microsoft.VisualStudio.Component.VC.Llvm.Clang --includeRecommended"
102-
winget install -e --id GoLang.Go
103-
winget install -e --id Git.Git
104-
```
100+
```powershell
101+
winget install -e --id Microsoft.VisualStudio.2022.Community --override "--passive --wait --add Microsoft.VisualStudio.Workload.NativeDesktop --add Microsoft.VisualStudio.Component.VC.Llvm.Clang --includeRecommended"
102+
winget install -e --id GoLang.Go
103+
winget install -e --id Git.Git
104+
```
105105

106106
3. Install vcpkg:
107107

108-
```powershell
109-
cd C:\
110-
git clone https://github.com/microsoft/vcpkg
111-
.\vcpkg\bootstrap-vcpkg.bat
112-
```
108+
```powershell
109+
cd C:\
110+
git clone https://github.com/microsoft/vcpkg
111+
.\vcpkg\bootstrap-vcpkg.bat
112+
```
113113

114114
4. [Download the latest version of the watcher library for Windows](https://github.com/e-dant/watcher/releases) and extract it to a directory named `C:\watcher`
115115
5. [Download the latest **Thread Safe** version of PHP and of the PHP SDK for Windows](https://windows.php.net/download/), extract them in directories named `C:\php` and `C:\php-devel`
116116
6. Clone the FrankenPHP Git repository:
117117

118-
```powershell
119-
git clone https://github.com/php/frankenphp C:\frankenphp
120-
cd C:\frankenphp
121-
```
118+
```powershell
119+
git clone https://github.com/php/frankenphp C:\frankenphp
120+
cd C:\frankenphp
121+
```
122122

123123
7. Install the dependencies:
124124

125-
```powershell
126-
C:\vcpkg\vcpkg.exe install
127-
```
125+
```powershell
126+
C:\vcpkg\vcpkg.exe install
127+
```
128128

129129
8. Configure the needed environment variables (PowerShell):
130130

131-
```powershell
132-
$env:PATH += ';C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\Llvm\bin'
133-
$env:CC = 'clang'
134-
$env:CXX = 'clang++'
135-
$env:CGO_CFLAGS = "-O0 -g -IC:\frankenphp\vcpkg_installed\x64-windows\include -IC:\watcher -IC:\php-devel\include -IC:\php-devel\include\main -IC:\php-devel\include\TSRM -IC:\php-devel\include\Zend -IC:\php-devel\include\ext"
136-
$env:CGO_LDFLAGS = '-LC:\frankenphp\vcpkg_installed\x64-windows\lib -lbrotlienc -LC:\watcher -llibwatcher-c -LC:\php -LC:\php-devel\lib -lphp8ts -lphp8embed'
137-
```
131+
```powershell
132+
$env:PATH += ';C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\Llvm\bin'
133+
$env:CC = 'clang'
134+
$env:CXX = 'clang++'
135+
$env:CGO_CFLAGS = "-O0 -g -IC:\frankenphp\vcpkg_installed\x64-windows\include -IC:\watcher -IC:\php-devel\include -IC:\php-devel\include\main -IC:\php-devel\include\TSRM -IC:\php-devel\include\Zend -IC:\php-devel\include\ext"
136+
$env:CGO_LDFLAGS = '-LC:\frankenphp\vcpkg_installed\x64-windows\lib -lbrotlienc -LC:\watcher -llibwatcher-c -LC:\php -LC:\php-devel\lib -lphp8ts -lphp8embed'
137+
```
138138

139139
9. Run the tests:
140140

141-
```powershell
142-
go test -race -ldflags '-extldflags="-fuse-ld=lld"' ./...
143-
cd caddy
144-
go test -race -ldflags '-extldflags="-fuse-ld=lld"' -tags nobadger,nomysql,nopgx ./...
145-
cd ..
146-
```
141+
```powershell
142+
go test -race -ldflags '-extldflags="-fuse-ld=lld"' ./...
143+
cd caddy
144+
go test -race -ldflags '-extldflags="-fuse-ld=lld"' -tags nobadger,nomysql,nopgx ./...
145+
cd ..
146+
```
147147

148148
10. Build the binary:
149149

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ Our maintainers offer apk packages for all systems using `apk`. To install, run:
9090
VERSION=85 # 82-85 available
9191
echo "https://pkg.henderkes.com/api/packages/${VERSION}/alpine/main/php-zts" | sudo tee -a /etc/apk/repositories
9292
KEYFILE=$(curl -sJOw '%{filename_effective}' https://pkg.henderkes.com/api/packages/${VERSION}/alpine/key)
93-
sudo mv ${KEYFILE} /etc/apk/keys/ &&
94-
sudo apk update &&
93+
sudo mv ${KEYFILE} /etc/apk/keys/ &&
94+
sudo apk update &&
9595
sudo apk add frankenphp
9696
```
9797

@@ -170,6 +170,7 @@ Go to `https://localhost`, and enjoy!
170170
- [Compile from sources](https://frankenphp.dev/docs/compile/)
171171
- [Monitoring FrankenPHP](https://frankenphp.dev/docs/metrics/)
172172
- [WordPress integration](https://frankenphp.dev/docs/wordpress/)
173+
- [Symfony integration](https://frankenphp.dev/docs/symfony/)
173174
- [Laravel integration](https://frankenphp.dev/docs/laravel/)
174175
- [Known issues](https://frankenphp.dev/docs/known-issues/)
175176
- [Demo app (Symfony) and benchmarks](https://github.com/dunglas/frankenphp-demo)
@@ -178,7 +179,7 @@ Go to `https://localhost`, and enjoy!
178179

179180
## Examples and Skeletons
180181

181-
- [Symfony](https://github.com/dunglas/symfony-docker)
182+
- [Symfony](https://frankenphp.dev/docs/symfony/)
182183
- [API Platform](https://api-platform.com/docs/symfony)
183184
- [Laravel](https://frankenphp.dev/docs/laravel/)
184185
- [Sulu](https://sulu.io/blog/running-sulu-with-frankenphp)

docs/symfony.md

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
# Symfony
2+
3+
## Docker
4+
5+
For [Symfony](https://symfony.com) projects, we recommend using [Symfony Docker](https://github.com/dunglas/symfony-docker), the official Symfony Docker setup maintained by FrankenPHP's author. It provides a complete Docker-based environment with FrankenPHP, automatic HTTPS, HTTP/2, HTTP/3, and worker mode support out of the box.
6+
7+
## Local Installation
8+
9+
Alternatively, you can run your Symfony projects with FrankenPHP from your local machine:
10+
11+
1. [Install FrankenPHP](../#getting-started)
12+
2. Add the following configuration to a file named `Caddyfile` in the root directory of your Symfony project:
13+
14+
```caddyfile
15+
# The domain name of your server
16+
localhost
17+
18+
root public/
19+
php_server {
20+
# Optional: Enable worker mode for better performance
21+
worker ./public/index.php
22+
}
23+
```
24+
25+
See the [performance documentation](performance.md) for further optimizations.
26+
27+
3. Start FrankenPHP from the root directory of your Symfony project: `frankenphp run`
28+
29+
## Worker Mode
30+
31+
Since Symfony 7.4, FrankenPHP worker mode is natively supported.
32+
33+
For older versions, install the FrankenPHP package of [PHP Runtime](https://github.com/php-runtime/runtime):
34+
35+
```console
36+
composer require runtime/frankenphp-symfony
37+
```
38+
39+
Start your app server by defining the `APP_RUNTIME` environment variable to use the FrankenPHP Symfony Runtime:
40+
41+
```console
42+
docker run \
43+
-e FRANKENPHP_CONFIG="worker ./public/index.php" \
44+
-e APP_RUNTIME=Runtime\\FrankenPhpSymfony\\Runtime \
45+
-v $PWD:/app \
46+
-p 80:80 -p 443:443 -p 443:443/udp \
47+
dunglas/frankenphp
48+
```
49+
50+
Learn more about [the worker mode](worker.md).
51+
52+
## Hot Reload
53+
54+
Hot reloading is enabled by default in [Symfony Docker](https://github.com/dunglas/symfony-docker).
55+
56+
To use the [hot reload](hot-reload.md) feature without Symfony Docker, enable [Mercure](mercure.md) and add the `hot_reload` sub-directive to the `php_server` directive in your `Caddyfile`:
57+
58+
```caddyfile
59+
localhost
60+
61+
mercure {
62+
anonymous
63+
}
64+
65+
root public/
66+
php_server {
67+
hot_reload
68+
worker ./public/index.php
69+
}
70+
```
71+
72+
Then, add the following code to your `templates/base.html.twig` file:
73+
74+
```twig
75+
{% if app.request.server.has('FRANKENPHP_HOT_RELOAD') %}
76+
<meta name="frankenphp-hot-reload:url" content="{{ app.request.server.get('FRANKENPHP_HOT_RELOAD') }}">
77+
<script src="https://cdn.jsdelivr.net/npm/idiomorph"></script>
78+
<script src="https://cdn.jsdelivr.net/npm/frankenphp-hot-reload/+esm" type="module"></script>
79+
{% endif %}
80+
```
81+
82+
Finally, run `frankenphp run` from the root directory of your Symfony project.
83+
84+
## Pre-Compressing Assets
85+
86+
Symfony's [AssetMapper component](https://symfony.com/doc/current/frontend/asset_mapper.html) can pre-compress assets with Brotli and Zstandard during deployment. FrankenPHP (through Caddy's `file_server`) can serve these pre-compressed files directly, avoiding on-the-fly compression overhead.
87+
88+
1. Compile and compress your assets:
89+
90+
```console
91+
php bin/console asset-map:compile
92+
```
93+
94+
2. Update your `Caddyfile` to serve pre-compressed assets:
95+
96+
```caddyfile
97+
localhost
98+
99+
@assets path /assets/*
100+
file_server @assets {
101+
precompressed zstd br gzip
102+
}
103+
104+
root public/
105+
php_server {
106+
worker ./public/index.php
107+
}
108+
```
109+
110+
The `precompressed` directive tells Caddy to look for pre-compressed versions of the requested file (e.g., `app.css.zst`, `app.css.br`) and serve them directly if the client supports it.
111+
112+
## Serving Large Static Files (`X-Sendfile`)
113+
114+
FrankenPHP supports [efficiently serving large static files](x-sendfile.md) after executing PHP code (for access control, statistics, etc.).
115+
116+
Symfony HttpFoundation [natively supports this feature](https://symfony.com/doc/current/components/http_foundation.html#serving-files).
117+
After [configuring your `Caddyfile`](x-sendfile.md#configuration), it will automatically determine the correct value for the `X-Accel-Redirect` header and add it to the response:
118+
119+
```php
120+
use Symfony\Component\HttpFoundation\BinaryFileResponse;
121+
122+
BinaryFileResponse::trustXSendfileTypeHeader();
123+
$response = new BinaryFileResponse(__DIR__.'/../private-files/file.txt');
124+
125+
// ...
126+
```
127+
128+
## Symfony Apps As Standalone Binaries
129+
130+
Using [FrankenPHP's application embedding feature](embed.md), it's possible to distribute Symfony
131+
apps as standalone binaries.
132+
133+
Follow these steps to prepare and package your Symfony app:
134+
135+
1. Prepare your app:
136+
137+
```console
138+
# Export the project to get rid of .git/, etc
139+
mkdir $TMPDIR/my-prepared-app
140+
git archive HEAD | tar -x -C $TMPDIR/my-prepared-app
141+
cd $TMPDIR/my-prepared-app
142+
143+
# Set proper environment variables
144+
echo APP_ENV=prod > .env.local
145+
echo APP_DEBUG=0 >> .env.local
146+
147+
# Remove the tests and other unneeded files to save space
148+
# Alternatively, add these files with the export-ignore attribute in your .gitattributes file
149+
rm -Rf tests/
150+
151+
# Install the dependencies
152+
composer install --ignore-platform-reqs --no-dev -a
153+
154+
# Optimize .env
155+
composer dump-env prod
156+
```
157+
158+
2. Create a file named `static-build.Dockerfile` in the repository of your app:
159+
160+
```dockerfile
161+
FROM --platform=linux/amd64 dunglas/frankenphp:static-builder-gnu
162+
# If you intend to run the binary on musl-libc systems, use static-builder-musl instead
163+
164+
# Copy your app
165+
WORKDIR /go/src/app/dist/app
166+
COPY . .
167+
168+
# Build the static binary
169+
WORKDIR /go/src/app/
170+
RUN EMBED=dist/app/ ./build-static.sh
171+
```
172+
173+
> [!CAUTION]
174+
>
175+
> Some `.dockerignore` files (e.g. default [Symfony Docker `.dockerignore`](https://github.com/dunglas/symfony-docker/blob/main/.dockerignore))
176+
> will ignore the `vendor/` directory and `.env` files. Be sure to adjust or remove the `.dockerignore` file before the build.
177+
178+
3. Build:
179+
180+
```console
181+
docker build -t static-symfony-app -f static-build.Dockerfile .
182+
```
183+
184+
4. Extract the binary:
185+
186+
```console
187+
docker cp $(docker create --name static-symfony-app-tmp static-symfony-app):/go/src/app/dist/frankenphp-linux-x86_64 my-app ; docker rm static-symfony-app-tmp
188+
```
189+
190+
5. Start the server:
191+
192+
```console
193+
./my-app php-server
194+
```
195+
196+
Learn more about the options available and how to build binaries for other OSes in the [applications embedding](embed.md)
197+
documentation.

docs/worker.md

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,28 +37,9 @@ frankenphp php-server --worker /path/to/your/worker/script.php --watch="/path/to
3737

3838
This feature is often used in combination with [hot reloading](hot-reload.md).
3939

40-
## Symfony Runtime
40+
## Symfony
4141

42-
> [!TIP]
43-
> The following section is only necessary prior to Symfony 7.4, where native support for FrankenPHP worker mode was introduced.
44-
45-
The worker mode of FrankenPHP is supported by the [Symfony Runtime Component](https://symfony.com/doc/current/components/runtime.html).
46-
To start any Symfony application in a worker, install the FrankenPHP package of [PHP Runtime](https://github.com/php-runtime/runtime):
47-
48-
```console
49-
composer require runtime/frankenphp-symfony
50-
```
51-
52-
Start your app server by defining the `APP_RUNTIME` environment variable to use the FrankenPHP Symfony Runtime:
53-
54-
```console
55-
docker run \
56-
-e FRANKENPHP_CONFIG="worker ./public/index.php" \
57-
-e APP_RUNTIME=Runtime\\FrankenPhpSymfony\\Runtime \
58-
-v $PWD:/app \
59-
-p 80:80 -p 443:443 -p 443:443/udp \
60-
dunglas/frankenphp
61-
```
42+
See [the dedicated documentation](symfony.md#worker-mode).
6243

6344
## Laravel Octane
6445

docs/x-sendfile.md

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,4 @@ header('X-Accel-Redirect: file.txt');
5858

5959
## Projects using the Symfony HttpFoundation component (Symfony, Laravel, Drupal...)
6060

61-
Symfony HttpFoundation [natively supports this feature](https://symfony.com/doc/current/components/http_foundation.html#serving-files).
62-
It will automatically determine the correct value for the `X-Accel-Redirect` header and add it to the response.
63-
64-
```php
65-
use Symfony\Component\HttpFoundation\BinaryFileResponse;
66-
67-
BinaryFileResponse::trustXSendfileTypeHeader();
68-
$response = new BinaryFileResponse(__DIR__.'/../private-files/file.txt');
69-
70-
// ...
71-
```
61+
See [the Symfony documentation](symfony.md#serving-large-static-files-x-sendfile) for details on using this feature with Symfony HttpFoundation.

0 commit comments

Comments
 (0)