yiisoft / yiisoft/router

Add ability to wrap action with middleware

Open
#282 0 comments 0 reactions 1 assignee View on GitHub

@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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.