PHPackages                             mspirkov/yii2-web - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. mspirkov/yii2-web

ActiveLibrary[Framework](/categories/framework)

mspirkov/yii2-web
=================

Yii2 Web extension.

0.4.0(3mo ago)391MITPHPPHP &gt;=7.4CI passing

Since Oct 30Pushed 1w ago1 watchersCompare

[ Source](https://github.com/mspirkov/yii2-web)[ Packagist](https://packagist.org/packages/mspirkov/yii2-web)[ RSS](/packages/mspirkov-yii2-web/feed)WikiDiscussions main Synced yesterday

READMEChangelog (6)Dependencies (22)Versions (8)Used By (0)

 [ ![](https://avatars0.githubusercontent.com/u/993323) ](https://github.com/yiisoft)

Yii2 Web Extension
==================

[](#yii2-web-extension)

A package of helper classes for working with web components in Yii2.

[![PHP](https://camo.githubusercontent.com/de6657ab22d88fd2e8c1fda7dbffa5716c7d53ea6250959be771ea72d4863315/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f253345253344372e342d3741383642382e7376673f7374796c653d666f722d7468652d6261646765266c6f676f3d706870266c6f676f436f6c6f723d7768697465266c6162656c3d504850)](https://www.php.net/releases/7_4_0.php)[![Yii 2.0.x](https://camo.githubusercontent.com/a8b86019a796c90e714cf9d9b730c74f7c01dfef6f1704e928cb1d991fad0065/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f253345253344322e302e35332d3234374241302e7376673f7374796c653d666f722d7468652d6261646765266c6f676f3d796969266c6f676f436f6c6f723d7768697465266c6162656c3d596969)](https://github.com/yiisoft/yii2/tree/2.0.53)[![Tests](https://camo.githubusercontent.com/ecdadf5c63e981f2052f47c9638ce69b63441c01dc8efa8a8a142113444b71a6/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6d737069726b6f762f796969322d7765622f63692e796d6c3f6272616e63683d6d61696e267374796c653d666f722d7468652d6261646765266c6f676f3d676974687562266c6162656c3d5465737473)](https://github.com/mspirkov/yii2-web/actions/workflows/ci.yml)[![PHPStan](https://camo.githubusercontent.com/081816bf35c4036da5c6e56c62d77f3dfae7e05a33c7aa489d0f320bb095c323/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6d737069726b6f762f796969322d7765622f63692e796d6c3f6272616e63683d6d61696e267374796c653d666f722d7468652d6261646765266c6f676f3d676974687562266c6162656c3d5048505374616e)](https://github.com/mspirkov/yii2-web/actions/workflows/ci.yml)[![Coverage](https://camo.githubusercontent.com/6ec077aa0bac0ac481f0e07cfdcb41ab11064cea82be02d7fb6f17027f9161b7/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f3130302532352d3434434331312e7376673f7374796c653d666f722d7468652d6261646765266c6162656c3d436f766572616765)](https://camo.githubusercontent.com/6ec077aa0bac0ac481f0e07cfdcb41ab11064cea82be02d7fb6f17027f9161b7/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f3130302532352d3434434331312e7376673f7374796c653d666f722d7468652d6261646765266c6162656c3d436f766572616765)[![PHPStan Level Max](https://camo.githubusercontent.com/347927812a31a0b0090fefb12765bc416adeb9383f8accf29740e8ffcdd43836/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4d61782d3741383642382e7376673f7374796c653d666f722d7468652d6261646765266c6162656c3d5048505374616e2532304c6576656c)](https://camo.githubusercontent.com/347927812a31a0b0090fefb12765bc416adeb9383f8accf29740e8ffcdd43836/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4d61782d3741383642382e7376673f7374796c653d666f722d7468652d6261646765266c6162656c3d5048505374616e2532304c6576656c)

Installation
------------

[](#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
----------

[](#components)

- [CookieManager](#cookiemanager)
- [Html](#html)
- [HtmlTrait](#htmltrait)
- [Request](#request)
- [RequestTrait](#requesttrait)

### CookieManager

[](#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

[](#usage-example)

##### Initialization

[](#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

[](#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

[](#html)

This package contains 4 helpers:

1. `MSpirkov\Yii2\Web\Html` - a helper that extends `yii\web\Html`
2. `MSpirkov\Yii2\Web\Bootstrap3\Html` - a helper that extends `yii\bootstrap\Html`
3. `MSpirkov\Yii2\Web\Bootstrap4\Html` - a helper that extends `yii\bootstrap4\Html`
4. `MSpirkov\Yii2\Web\Bootstrap5\Html` - a helper that extends `yii\bootstrap5\Html`

Important

To use Bootstrap helpers, you need to install the corresponding packages ([yii2-bootstrap](https://github.com/yiisoft/yii2-bootstrap), [yii2-bootstrap4](https://github.com/yiisoft/yii2-bootstrap4), or [yii2-bootstrap5](https://github.com/yiisoft/yii2-bootstrap5))

All of them contain methods from the [HtmlTrait](#htmltrait) and allow you to use its features without having to create your own basic helper.

### HtmlTrait

[](#htmltrait)

A trait that extends the basic functionality of the `\yii\helpers\Html` helper.

#### Usage example

[](#usage-example-1)

```
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`

[](#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:

```
