webscooby/yii-web Add support injection view parameters, psr http messages and psr middleware for yii2

  1. Webscooby yii-web component
  2. Installation
  3. Using
  4. Requirements

Webscooby yii-web component

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.
1 0
1 follower
65 downloads
Yii Version: 2.0
License: MIT
Category: Others
Tags:
Developed by: Lex
Created on: Apr 27, 2021
Last updated: 3 years ago
Packagist Profile
Github Repository