All jobs

Add Twig Templates to a Slim 4 App

Yes. An agent installs slim/twig-view, registers Twig in the PHP-DI container, renders a route from a template, keeps PHPUnit, PHPStan and phpcs green, in a PR.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

The agent reads the skeleton before adding to it. In a project laid out like slimphp/Slim-Skeleton, composer.json requires slim/slim ^4.12, slim/psr7 ^1.6, php-di/php-di ^6.4 and monolog/monolog ^2.9 on PHP ^7.4 || ^8.0, with phpunit/phpunit ^9.6.17, phpstan/phpstan ^1.10 and squizlabs/php_codesniffer ^3.9 in require-dev. public/index.php builds a PHP-DI container from three files, app/settings.php, app/dependencies.php and app/repositories.php, then calls AppFactory::create(), registers app/middleware.php and app/routes.php, and adds the routing, body-parsing and error middleware. app/routes.php answers / by writing the string Hello world! straight to the response body and groups /users under two action classes. app/dependencies.php defines only LoggerInterface. .gitignore excludes composer.lock and var/cache/*, and .github/workflows/tests.yml runs vendor/bin/phpunit --coverage-clover clover.xml on PHP 7.4, 8.0, 8.1 and 8.2, with vendor/bin/phpcs and vendor/bin/phpstan on 8.1.

The package is slim/twig-view, installed with composer require slim/twig-view; its current release, 3.4.1, requires Slim ^4.12, twig/twig ^3.11 and PHP ^7.4 or 8.0 through 8.5, which fits this skeleton's constraint. The agent adds a Twig::class definition to app/dependencies.php that returns Twig::create(__DIR__ . '/../templates', ['cache' => false]), reading the cache path from the settings object instead when the prompt asks for a compiled-template cache under var/cache. In app/middleware.php it adds TwigMiddleware::create($app, $app->getContainer()->get(Twig::class)) next to the existing SessionMiddleware; the createFromContainer form expects a view key, which this container does not define, so the explicit form is the one that matches the skeleton's class-keyed definitions.

Templates go in a new templates/ directory: a layout.twig with the HTML skeleton and a home.twig that extends it. The / route in app/routes.php changes from writing Hello world! to return Twig::fromRequest($request)->render($response, 'home.twig', ['name' => 'world']), which keeps the same words on the page while proving the template path. The extension slim/twig-view registers gives templates url_for(), full_url_for(), is_current_url(), current_url(), get_uri() and base_path(), so the layout links to the /users group by route name once the agent names those routes with setName() rather than hard-coding paths.

Tests follow the shape already in tests/. tests/TestCase.php builds the app from the same three files plus middleware and routes, so the new Twig definition is exercised without a separate bootstrap. The agent adds a HomeActionTest that calls createRequest('GET', '/'), dispatches it through $app->handle(), and asserts the body contains the rendered greeting and that Content-Type is HTML. It then runs composer test, vendor/bin/phpstan and vendor/bin/phpcs in the sandbox; phpcs enforces the phpcs.xml standard on the new files, and PHPStan needs the templates directory to exist at analysis time because Twig::create is called inside a closure. Nothing in src/Application/Actions changes: the JSON actions keep returning ActionPayload through respondWithData().

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "Add Twig views to this Slim 4 app with slim/twig-view. Read composer.json, public/index.php, app/settings.php, app/dependencies.php, app/middleware.php, app/routes.php, tests/TestCase.php, phpcs.xml, phpstan.neon.dist and .github/workflows/tests.yml first. Run composer require slim/twig-view. Define Twig::class in app/dependencies.php pointing at a new templates/ directory with the cache off in development, add the TwigMiddleware in app/middleware.php using the explicit create form with the container's Twig instance, create templates/layout.twig and templates/home.twig, and change the GET / route to render home.twig with the same greeting it writes today. Name the existing routes so templates can use url_for. Add a PHPUnit test that requests / and asserts the rendered body and content type. Run composer test, vendor/bin/phpstan and vendor/bin/phpcs, fix what they report, and open a pull request that quotes their output.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
  1. Get an API key. In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
  2. Send the request. POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
  3. Read what came back. Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

What comes back

The pull request touches composer.json (one new requirement), app/dependencies.php, app/middleware.php, app/routes.php, the two new files under templates/, and one new test class. composer.lock is not in the diff because this repository ignores it; the description says so and names the version of slim/twig-view that resolved in the sandbox so a reviewer can pin it if they want to. The description also quotes the PHPUnit, PHPStan and phpcs output.

A reviewer checks that the middleware is added once, after the session middleware the skeleton already has, that the / route still returns the same greeting, and that the template cache is off in development and, if enabled, writes under var/cache, which .gitignore already covers. They may ask for the /users JSON routes to stay untouched, which they are.

After merge, composer start serves / from a template, and the four PHP versions in tests.yml run the new test on the same ramsey/composer-install step as before.

Run it on every Linear issue labelled templates

A team that is moving a JSON-only Slim service toward server-rendered pages can file one Linear issue per page. An automation on the Project with a linear trigger on linear.issue.labeled for the templates label starts a read-only run that reads the routes and comments on the issue with the template it would add and the route it would change. Nothing is pushed by that run.

When the plan is accepted, a handler on the reader's server sends this body to POST /o/{org}/sessions with the issue's URL added to resources and a request_id that is a UUID v5 of the issue identifier, so a retried call returns the run it already started. That run makes the change, runs the three checks and opens the pull request that links the issue.

The use case this job follows, with its automation and trigger: Hand off a long task.

Why run it on Rush?

Adding a template engine touches the container, the middleware stack and the routes at once, and the skeleton's three checks each catch a different mistake: PHPUnit catches a wrong template path, PHPStan catches a closure that returns the wrong type, and phpcs catches a file that does not match phpcs.xml. The agent runs all three in its own sandbox on the PHP version the matrix uses for analysis and keeps the output with the pull request.

A team with several Slim services gives each one its own run. The services differ in where their settings live and whether composer.lock is committed; each run reads its own files and reports what it found, rather than one script assuming every skeleton is unmodified.

Frequently asked questions

Why not use TwigMiddleware::createFromContainer?

That form looks the Twig instance up under the container key view, and this skeleton registers services by class name through PHP-DI definitions. The agent uses TwigMiddleware::create($app, $container->get(Twig::class)) so the definition in app/dependencies.php is the only place Twig is configured. Adding a view alias instead is a one-line change if a reviewer prefers it.

Does this change the JSON responses under /users?

No. ListUsersAction and ViewUserAction keep extending Action and returning ActionPayload as JSON. Only the / route is rendered through Twig, and the two existing action tests keep passing unchanged.

Where does the compiled template cache go?

Off in development, which is what Twig::create gets with 'cache' => false. When the prompt asks for a cache, the agent points it at var/cache/twig, which the existing .gitignore rule for var/cache/* already excludes, and reads the path from the settings object so production can change it.

Which PHP versions does slim/twig-view support here?

Its composer.json allows PHP ^7.4 and 8.0 through 8.5 and requires twig/twig ^3.11, so every version in the project's CI matrix, 7.4 through 8.2, resolves. The agent runs the checks in the sandbox and quotes the resolved versions in the pull request.

More Slim jobs

More jobs like this: Hand off a long task

Keep reading