Add ability to wrap action with middleware
Open
@rustamwin is already working on this.
Since Apr 9, 2026.
type:feature
- Dominant language
- PHP
- Stars
- 63
- Forks
- 26
- Avg merge
- 6d 2h
- Merged PRs (30d)
- 1
Description
Proposed new feature or change
The idea is to allow non-PSR inputs and outputs for a route:
public function myAction(string $name): array
{
return ['name' => $name];
}
Not to do it directly in the router, we can allow specifying a middleware that wraps the action, converting PSR inputs to action arguments and action output to PSR output.
https://github.com/yiisoft/router/blob/master/src/Middleware/Router.php#L57
$middlewares = $result->route()->getData('enabledMiddlewares');
$action = array_pop($middlewares);
if ($this->actionWrapper) {
$middlewares[] = new $this->actionWrapper($action);
}
...
The wrapper itself can be the following:
final class ActionWrapperMiddleware()
{
public function __construct(private $action)
{
}
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$params = $this->getFromRequest($request);
// TODO: if action is Middleware or has middleware signature, return it as is
$data = $this->action(...$params);
$handler->handle(new DataResponse($data));
}
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.