Skip to content

Release 0.3.0

Latest
Compare
Choose a tag to compare
@ezimuel ezimuel released this 13 Sep 20:56
· 1 commit to main since this release
0.3.0
39ed176

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 and AttributeTrait 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!