webscooby/yii-web

Add support injection view parameters, psr http messages and psr middleware for yii2

1.0.3 2021-04-29 10:25 UTC

This package is auto-updated.

Last update: 2021-04-29 10:26:15 UTC


README

Latest Stable Version Latest Unstable Version Build Status codecov

Add supports PSR-7 http messages, and PSR-15 middlewares. Also add the ability to add parameters to views via objects, that implement ContentParametersInjectionInterface.

Installation

  1. Use composer.
composer require webscooby/yii-web
  1. Add WebRequestFactory to bootstrap. For example, add to your config:
use Webscooby\Yii\Web\WebRequestFactory;
return [
    'bootstrap' => [
        WebRequestFactory::class
    ]
];
  1. Add psr-17 to configuration. Recommend using httpsoft/http-message

Using

  • For using middlewares attach MiddlewareDispatcherBehavior to controller or module. Example:
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;use Webscooby\Yii\Web\MiddlewareDispatcherBehavior;
class SomeController extends Controller {
    public function behaviors(): array
    {
        return [
            //Middleware can be added as class, object or callback
            [
                'class' => MiddlewareDispatcherBehavior::class,
                'middlewares' => [SomeMiddleware::class, $someMiddleware]
            ],
            //Also middleware can be limited for the specified action 
            [
                'class' => MiddlewareDispatcherBehavior::class,
                'actions' => ['index'],
                'middlewares' => [
                    static function (ServerRequestInterface $request, RequestHandlerInterface $handler) {
                        ...
                    }
                ]
            ]
        ];
    }
}
  • For adding parameters to view, change view class and add your injections. Example:
<?php

use Webscooby\Security\User\UserViewInjection;
use Webscooby\Yii\Web\View;

return [
    'components' => [
        'view' => [
            'class' => View::class,
            'injections' => [
                UserViewInjection::class,
                static function(): array {
                    return [
                        ...
                    ];
                },
                $injector
            ]
        ]
    ]
];

Requirements

  • PHP 7.4 or higher.