What the agent does
The agent reads how requests flow before it adds headers. In a Slim 4 app 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 for development. public/index.php builds the PHP-DI container from app/settings.php, app/dependencies.php and app/repositories.php, creates the app with AppFactory, runs app/middleware.php (which adds only SessionMiddleware), runs app/routes.php, then adds the routing middleware, the body parsing middleware and the error middleware with HttpErrorHandler. app/routes.php already has $app->options('/{routes:.*}', …) returning an empty response under a comment calling it the CORS preflight handler, plus GET /, GET /users and GET /users/{id}, but nothing sets an Access-Control-* header, so a browser on another origin still refuses the response. tests/TestCase.php builds the app the same way and offers createRequest($method, $path, $headers); tests.yml runs phpcs, phpstan and phpunit across PHP 7.4 to 8.2.
The agent adds src/Application/Middleware/CorsMiddleware.php implementing Psr\Http\Server\MiddlewareInterface like SessionMiddleware, with its options injected from a new cors block in app/settings.php: origins read from a comma-separated CORS_ORIGINS environment variable (empty means no origin is allowed), methods, headers and maxAge. process() reads the request's Origin header; when it matches the allowlist exactly, the response gets Access-Control-Allow-Origin echoing that origin, Vary: Origin, and Access-Control-Allow-Credentials: true only because the list is explicit, never with a wildcard. For a preflight, an OPTIONS request with Access-Control-Request-Method, it returns 204 with Access-Control-Allow-Methods, Access-Control-Allow-Headers echoing Access-Control-Request-Headers, and Access-Control-Max-Age, without calling the handler. The existing OPTIONS /{routes:.*} route stays, so the routing middleware never answers a preflight with 405.
Where the middleware is added decides whether error responses carry the headers. Slim runs middleware last-added-first, and public/index.php adds the error middleware after app/middleware.php has run, so a CorsMiddleware added there would sit inside the error handler and a 404 or a 500 built by HttpErrorHandler would come back without Access-Control-Allow-Origin, which a browser reports as a CORS failure rather than the real error. The agent therefore adds $app->add(CorsMiddleware::class) in public/index.php after addErrorMiddleware, mirrors that in tests/TestCase.php::getAppInstance, and leaves app/middleware.php to SessionMiddleware. docker-compose.yml gets CORS_ORIGINS beside the existing docker environment variable and README.md documents it.
Tests go in tests/Application/Middleware/CorsMiddlewareTest.php using the createRequest helper with an Origin header: a preflight from an allowed origin gets 204 and the three preflight headers; a GET /users from an allowed origin gets the data and Access-Control-Allow-Origin plus Vary; the same from an origin not in the list gets the data with no CORS header; and GET /missing from an allowed origin gets the 404 payload with the header, proving the ordering. The agent runs composer install, vendor/bin/phpunit, vendor/bin/phpstan and vendor/bin/phpcs in its sandbox and keeps the output; phpcs.xml and phpstan.neon.dist are respected rather than changed.
Start it with one API call
POST https://api.phoenix.so/o/{org}/sessions with this body:
{
"agent": "claude",
"input": "Add CORS to this Slim 4 API. Read composer.json, public/index.php, app/middleware.php, app/routes.php, app/settings.php, the existing middleware class, tests/TestCase.php and the CI workflow first. Write a PSR-15 CorsMiddleware configured from a cors block in settings with origins from a CORS_ORIGINS environment variable, exact-match origins only, Vary: Origin, credentials only with an explicit list, and a 204 preflight answer with methods, headers and max-age. Keep the existing OPTIONS catch-all route. Add the middleware so that it is outermost, after the error middleware, in both index.php and the test app factory, and explain the ordering in the PR. Add PHPUnit tests for an allowed preflight, an allowed GET, a disallowed origin and a 404 that still carries the header. Run phpunit, phpstan and phpcs and include the output. Document the variable in the README and docker-compose. Open a pull request.",
"resources": [
{
"url": "https://github.com/your-org/your-app"
}
]
}- 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.
- 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.
- 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
What comes back is a pull request that adds src/Application/Middleware/CorsMiddleware.php and tests/Application/Middleware/CorsMiddlewareTest.php, and changes app/settings.php (the cors block), public/index.php and tests/TestCase.php (the outermost add), docker-compose.yml and README.md. app/routes.php, app/middleware.php and composer.json are unchanged.
The description explains the last-added-first ordering and why the middleware sits outside the error handler, lists the four tests, and quotes the phpunit, phpstan and phpcs results. The commands and full output are in the transcript at GET /o/{org}/sessions/{id}/messages.
Before merging, a reviewer sets CORS_ORIGINS for each environment, decides whether credentials are needed at all, and checks that the allowed headers cover what the front end sends, such as Authorization and Content-Type. A follow-up message to the same session can add wildcard subdomain matching or move the list into a config file.
Run it on every PHP API called from a browser on another origin
A team with several Slim services starts one run per repository with one POST /o/{org}/sessions each, the repository in resources and this prompt as input. Each run reads its own bootstrap, so an app that registers middleware in a different order gets the add placed where it is still outermost, and one that already has an OPTIONS route keeps it.
Send a request_id, a UUID v5 of the repository's full name, so a retried call returns the run it already started. The key's webhook_url reports when each run the key starts finishes, with the run's final answer in result.
The use case this job follows, with its automation and trigger: Hand off a long task.
Why run it on Rush?
CORS bugs come from ordering and edge cases: headers missing on errors, a preflight answered 405 by the router, credentials sent with a wildcard. The agent reads the exact middleware order in index.php, places the class where errors still get the headers, and proves each case with PHPUnit in a sandbox of its own, with the output kept.
The pull request carries the middleware, the ordering explanation and the tests, so review is a diff rather than a browser console and a guess. Several services can be done at once, each in its own sandbox with its own pull request.
Frequently asked questions
Why not use a CORS package from Packagist?
The skeleton has one middleware class and a PSR-15 interface already; the behaviour needed is under a hundred lines and testable with the existing helper. If you prefer a package, say which one and the agent wires it with the same settings and tests.
Will a 404 or 500 still trigger a CORS error in the browser?
No. Because the middleware is added after the error middleware it is outermost, so the response HttpErrorHandler builds passes back through it and gets Access-Control-Allow-Origin; one test asserts this on GET /missing.
Can I allow every origin?
Set CORS_ORIGINS=* and the middleware answers Access-Control-Allow-Origin: * without the credentials header, because a wildcard with credentials is refused by browsers. The agent recommends an explicit list and says so in the README.
Does the preflight hit the session middleware?
No. The CORS middleware answers an OPTIONS preflight itself with 204 before calling the next handler, so SessionMiddleware, routing and the action never run for it.
More Slim jobs
The same job on other stacks
- Add CORS to a Socket.IO Server (Socket.IO)
- Add CORS to a tRPC API (tRPC)
- Add CORS to a Vapor Server (Vapor)