Skip to content

Commit 7f74d29

Browse files
authored
Create README.md
1 parent 9747465 commit 7f74d29

File tree

1 file changed

+156
-0
lines changed

1 file changed

+156
-0
lines changed

README.md

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
<h1 align="center">Yii2 WEB</h1>
2+
3+
A package of helper classes for working with web components in Yii2.
4+
5+
[![PHP](https://img.shields.io/badge/%3E%3D7.4-7A86B8.svg?style=for-the-badge&logo=php&logoColor=white&label=PHP)](https://www.php.net/releases/7_4_0.php)
6+
[![Yii 2.0.x](https://img.shields.io/badge/%3E%3D2.0.53-247BA0.svg?style=for-the-badge&logo=yii&logoColor=white&label=Yii)](https://github.com/yiisoft/yii2/tree/2.0.53)
7+
[![Tests](https://img.shields.io/github/actions/workflow/status/mspirkov/yii2-web/ci.yml?branch=main&style=for-the-badge&logo=github&label=Tests)](https://github.com/mspirkov/yii2-web/actions/workflows/ci.yml)
8+
[![PHPStan](https://img.shields.io/github/actions/workflow/status/mspirkov/yii2-web/ci.yml?branch=main&style=for-the-badge&logo=github&label=PHPStan)](https://github.com/mspirkov/yii2-web/actions/workflows/ci.yml)
9+
![Coverage](https://img.shields.io/badge/100%25-44CC11.svg?style=for-the-badge&label=Coverage)
10+
![PHPStan Level Max](https://img.shields.io/badge/Max-7A86B8.svg?style=for-the-badge&label=PHPStan%20Level)
11+
12+
## Installation
13+
14+
Run
15+
16+
```bash
17+
php composer.phar require mspirkov/yii2-web
18+
```
19+
20+
or add
21+
22+
```json
23+
"mspirkov/yii2-web": "^0.1"
24+
```
25+
26+
to the `require` section of your `composer.json` file.
27+
28+
## Usage
29+
30+
### Request
31+
32+
A wrapper for `\yii\web\Request` for easier handling of **GET** and **POST** parameters.
33+
34+
#### Configuration
35+
36+
First, you need to replace the `request` component in the configuration:
37+
38+
```php
39+
<?php
40+
41+
use MSpirkov\Yii2\Web\Request;
42+
43+
return [
44+
...
45+
'components' => [
46+
'request' => [
47+
'class' => Request::class,
48+
],
49+
...
50+
],
51+
];
52+
```
53+
54+
#### IDE Autocomplete (Optional)
55+
56+
You also need to specify this class in `__autocomplete.php` so that the IDE knows which class to use:
57+
58+
```php
59+
<?php
60+
61+
use yii\BaseYii;
62+
use yii\web\Application;
63+
use MSpirkov\Yii2\Web\Request;
64+
65+
class Yii extends BaseYii
66+
{
67+
/** @var __Application */
68+
public static $app;
69+
}
70+
71+
/**
72+
* @property-read Request $request
73+
*/
74+
class __Application extends Application {}
75+
```
76+
77+
#### Basic Controller (Optional)
78+
79+
I also recommend that you create your own basic controller and specify `Request` there:
80+
81+
```php
82+
use MSpirkov\Yii2\Web\Request;
83+
84+
/**
85+
* @property Request $request
86+
*/
87+
class Controller extends \yii\web\Controller
88+
{
89+
public function init(): void
90+
{
91+
parent::init();
92+
93+
$this->request = Instance::ensure($this->request, Request::class);
94+
}
95+
}
96+
```
97+
98+
#### Usage example:
99+
100+
```php
101+
class ProductController extends Controller
102+
{
103+
/**
104+
* @return array{success: bool, message?: string}
105+
*/
106+
public function actionDelete(): array
107+
{
108+
$this->response->format = Response::FORMAT_JSON;
109+
if (!$this->request->isAjax) {
110+
throw new BadRequestHttpException();
111+
}
112+
113+
return $this->service->delete($this->request->getPostInt('id'));
114+
}
115+
}
116+
```
117+
118+
### CookieManager
119+
120+
A utility class for managing cookies.
121+
122+
This class encapsulates the logic for adding, removing, checking existence, and retrieving cookies, using the `\yii\web\Request`
123+
and `\yii\web\Response` objects. It simplifies working with cookies by abstracting implementation details and providing more
124+
convenient methods.
125+
126+
Usage example:
127+
128+
```php
129+
class CookieManager extends \MSpirkov\Yii2\Web\CookieManager
130+
{
131+
public function __construct()
132+
{
133+
parent::__construct(
134+
Instance::ensure('request', Request::class),
135+
Instance::ensure('response', Response::class),
136+
);
137+
}
138+
}
139+
```
140+
141+
```php
142+
class ExampleService
143+
{
144+
public function __construct(
145+
private readonly CookieManager $cookieManager,
146+
) {}
147+
148+
public function doSomething()
149+
{
150+
$this->cookieManager->add([
151+
'name' => 'someCookieName',
152+
'value' => 'someCookieValue',
153+
]);
154+
}
155+
}
156+
```

0 commit comments

Comments
 (0)