1: 设置
在本教程中,您将构建 Phlo Poll:一个小型投票应用程序,询问“哪个技术栈获胜?”。您从一个空文件夹开始,最终得到一个样式化的、多语言的、实时的投票应用。每一章都增加一层:一个 route,一个 view,CSS,数据,投票,async 更新,翻译和实时结果。在第一章中,您将安装 Phlo,编写您的第一个 route,并在浏览器中查看它。
1.1: 使用 Docker 安装
运行 Phlo 的最快方法是使用官方 Docker 镜像。它包含 PHP、FrankenPHP 网络服务器和位于 /phlo 的 Phlo 引擎。将一个新应用程序搭建到 ./app 中:
docker run -it -v $(pwd)/app:/app ghcr.io/q-ainl/phlo php /phlo/install.php /app
安装程序会询问几个问题。请这样回答:
App name: Poll
Host: localhost
Purpose (one line): Which stack wins?
Resources (comma-separated, empty for none): <press enter>
Create "Poll" in /app for host localhost? y
现在将资源留空;当应用程序需要它们时再添加。安装程序会生成项目结构(app.phlo、www/app.php、data/app.json),并以干净的构建完成,因此您总是从零开始。
1.2: 启动服务器
作为网络服务器运行相同的镜像:
docker run -p 80:80 -v $(pwd)/app:/app ghcr.io/q-ainl/phlo
在浏览器中打开 http://localhost。您会看到带有应用名称和您的单行目的的占位符主页。该页面来自于已搭建的 app.phlo,其中已经包含了一个 route、一个 view 和一个样式块。保持原样;投票将有自己的文件。
1.3: Your first route
Every .phlo file transpiles to exactly one PHP class, named after the file. Create app/poll.phlo (next to app.phlo) with one line:
route GET hello => view('Hello')
Three things to notice:
- Route paths use spaces, not slashes.
route GET poll votematches/poll/vote. - No semicolons. A line ending terminates a statement in Phlo.
view(...)renders and terminates. A route must end inview(),apply(), orlocation(). A bare return value is discarded, soroute GET hello => 'Hello'would match but render an empty page.
Routes from all files are collected automatically; the scaffolded app.phlo activates them with app::route(). Save the file and reload the browser: nothing breaks, the new route is just not built yet.
1.4: Build and check
In development (build: true) Phlo rebuilds changed sources on every request, so reloading the browser is usually enough. The CLI gives you the same build explicitly, plus a lint check. The CLI runs inside the container, because www/app.php points at the engine at /phlo:
docker run --rm -v $(pwd)/app:/app ghcr.io/q-ainl/phlo php /app/www/app.php build::run
docker run --rm -v $(pwd)/app:/app ghcr.io/q-ainl/phlo php /app/www/app.php build::lint
The first command prints the files it transpiled:
["*app.php","+poll.php","*classmap.php","*sourcemap.php"]
Run it again and it returns []: everything is built, nothing changed. build::lint must also return []; that means the transpiled PHP parses cleanly. From here on, the chapters write the short form php www/app.php build::run; prefix it with the Docker command above if you use the Docker setup.
Now open http://localhost/hello. The browser shows a minimal page with the text Hello. One line of Phlo, one route, one page. In the next chapter you replace it with a real view.