mspirkov / yii2-web
Yii2 Web extension.
Requires
- php: >=7.4
- yiisoft/yii2: ^2.0.53
Requires (Dev)
- ergebnis/phpstan-rules: ^2.13
- friendsofphp/php-cs-fixer: ^3.89
- phpstan/phpstan: ^2.1
- phpstan/phpstan-phpunit: ^2.0
- phpstan/phpstan-strict-rules: ^2.0
- phpunit/phpunit: ^9.6
- rector/rector: ^2.2
- shipmonk/phpstan-rules: ^4.2
- spaze/phpstan-disallowed-calls: ^4.7
- yiisoft/yii2-bootstrap: ^2.0
- yiisoft/yii2-bootstrap4: ^2.0
- yiisoft/yii2-bootstrap5: ^2.0
Suggests
- yiisoft/yii2-bootstrap: To use classes related to Bootstrap3
- yiisoft/yii2-bootstrap4: To use classes related to Bootstrap4
- yiisoft/yii2-bootstrap5: To use classes related to Bootstrap5
This package is auto-updated.
Last update: 2026-03-13 06:08:04 UTC
README
Yii2 Web Extension
A package of helper classes for working with web components in Yii2.
Installation
Run
php composer.phar require mspirkov/yii2-web
or add
"mspirkov/yii2-web": "^0.4"
to the require section of your composer.json file.
Components
CookieManager
A utility class for managing cookies.
This class encapsulates the logic for adding, removing, checking existence, and retrieving cookies, using the \yii\web\Request
and \yii\web\Response objects. It simplifies working with cookies by abstracting implementation details and providing more
convenient methods.
It contains the following methods:
has- checks if a cookie with the specified name exists.get- returns the cookie with the specified name.add- adds a cookie to the response.remove- removes a cookie.removeAll- removes all cookies.
Usage example
Initialization
Add the definition to the container configuration in the definitions section:
use MSpirkov\Yii2\Web\CookieManagerInterface; use MSpirkov\Yii2\Web\CookieManager; use MSpirkov\Yii2\Web\Request; use yii\web\Response; return [ ... 'container' => [ 'definitions' => [ CookieManagerInterface::class => static fn() => new CookieManager( Instance::ensure('request', Request::class), Instance::ensure('response', Response::class), ), ], ], ... ];
Usage
use MSpirkov\Yii2\Web\CookieManagerInterface; final readonly class ExampleService { public function __construct( private CookieManagerInterface $cookieManager, ) {} public function addCookie(): void { $this->cookieManager->add([ 'name' => 'someCookieName', 'value' => 'someCookieValue', ]); } }
Html
This package contains 4 helpers:
MSpirkov\Yii2\Web\Html- a helper that extendsyii\web\HtmlMSpirkov\Yii2\Web\Bootstrap3\Html- a helper that extendsyii\bootstrap\HtmlMSpirkov\Yii2\Web\Bootstrap4\Html- a helper that extendsyii\bootstrap4\HtmlMSpirkov\Yii2\Web\Bootstrap5\Html- a helper that extendsyii\bootstrap5\Html
Important
To use Bootstrap helpers, you need to install the corresponding packages (yiisoft/yii2-bootstrap, yii2-bootstrap4, or yii2-bootstrap5)
All of them contain methods from the HtmlTrait and allow you to use its features without having to create your own basic helper.
HtmlTrait
A trait that extends the basic functionality of the \yii\helpers\Html helper.
Usage example
use MSpirkov\Yii2\Web\HtmlTrait; final class Html extends \yii\helpers\Html { use HtmlTrait; }
You can also use this trait with other helpers that extends \yii\helpers\Html. For example:
use MSpirkov\Yii2\Web\HtmlTrait; final class Html extends \yii\bootstrap5\Html { use HtmlTrait; }
Method singleButtonForm
Сreates a form as a single button with hidden inputs. This can be useful when you need to perform an action when you click a button, such as deleting an item. This allows you to easily perform a request without manually creating a form, hidden inputs, etc.
Usage example:
<?= Html::singleButtonForm(['product/delete'], ['id' => $product->id], 'Delete'); ?>
Request
A wrapper for \yii\web\Request that uses the capabilities of RequestTrait
and allows you to use them without having to create your own basic Request.
Configuration
First, you need to replace the request component in the configuration:
use MSpirkov\Yii2\Web\Request; return [ ... 'components' => [ 'request' => [ 'class' => Request::class, ... ], ... ], ];
IDE Autocomplete (Optional)
You also need to specify this class in __autocomplete.php so that the IDE knows which class to use:
<?php use yii\BaseYii; use yii\web\Application as BaseWebApplication; use yii\console\Application as BaseConsoleApplication; use MSpirkov\Yii2\Web\Request; final class Yii extends BaseYii { /** @var __WebApplication|__ConsoleApplication */ public static $app; } /** * @property-read Request $request */ final class __WebApplication extends BaseWebApplication {} final class __ConsoleApplication extends BaseConsoleApplication {}
Basic Controller (Optional)
I also recommend that you create your own basic controller and specify Request there:
use MSpirkov\Yii2\Web\Request; use yii\web\Controller as BaseController; /** * @property Request $request */ abstract class AbstractController extends BaseController { public function init(): void { parent::init(); $this->request = Instance::ensure($this->request, Request::class); } }
Usage example
final class ProductController extends AbstractController { public function __construct( string $id, Module $module, private readonly ProductService $service, array $config = [], ) { parent::__construct($id, $module, $config); } public function actionDelete(): array { $this->response->format = Response::FORMAT_JSON; return $this->service->delete($this->request->getPostInt('id')); } }
RequestTrait
A trait for easier handling of GET and POST parameters.
Important
All parameter retrieval methods also allow you to mark parameters as required using the $required parameter.
It contains the following methods:
getGetInt- gets the value of a GET parameter by its name and tries to convert it to an integer.getGetFloat- gets the value of the GET parameter by its name and tries to convert it to a floating-point number.getGetBool- gets the value of the GET parameter by its name and tries to convert it to a boolean.getGetString- gets the value of the GET parameter by its name and tries to convert it to a string.getGetArray- gets the value of the GET parameter by its name and tries to convert it to an array.getPostInt- gets the value of a POST parameter by its name and tries to convert it to an integer.getPostFloat- gets the value of the POST parameter by its name and tries to convert it to a floating-point number.getPostBool- gets the value of the POST parameter by its name and tries to convert it to a boolean.getPostString- gets the value of the POST parameter by its name and tries to convert it to a string.getPostArray- gets the value of the POST parameter by its name and checks that the value is an array.
Usage example
use MSpirkov\Yii2\Web\RequestTrait; class Request extends \yii\web\Request { use RequestTrait; }