This is the 0.3.0 release of SimpleMVC framework.
This release includes the following fixes and addition:
- Fixed the controller pipeline routing, using an array of controllers in the route #2
- Added the
AttributeInterface
andAttributeTrait
for passing attributes between controllers in a pipeline #2
Now you can pass PSR-7 attributes to $request
between controllers using the addRequestAttribute()
function of AttributeInterface
. For instance, if you have a [Controller\A::class, Controller\B:class]
route pipeline, you can pass a foo
attribute between A
and B
controller as follows:
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use SimpleMVC\Controller\AttributeInterface;
use SimpleMVC\Controller\AttributeTrait;
use SimpleMVC\Controller\ControllerInterface;
class A implements ControllerInterface, AttributeInterface
{
use AttributeTrait;
public function execute(
ServerRequestInterface $request,
ResponseInterface $response
): ResponseInterface
{
$this->addRequestAttribute('foo', 'bar');
return $response;
}
}
In order to get the foo
parameter in the B
controller you can use the PSR-7 standard function getAttribute()
from the HTTP request, as follows:
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use SimpleMVC\Controller\ControllerInterface;
class B implements ControllerInterface
{
public function execute(
ServerRequestInterface $request,
ResponseInterface $response
): ResponseInterface
{
$attribute = $request->getAttribute('foo');
pritnf("Attribute is: %s", $attribute); // bar
return $response;
}
}
Note that you don't need to implement the AttributeInterface
for the B
controller since we only need to read from the $request
.
A special thanks to @danielebarbaro for helping in this release!