Skip to content

Commit 9bacab9

Browse files
hacan359hacan359samdark
authored
Add RequestCookies doc information. Add view injection docs (#225)
Co-authored-by: hacan359 <[email protected]> Co-authored-by: Alexander Makarov <[email protected]>
1 parent 01f32eb commit 9bacab9

File tree

3 files changed

+132
-0
lines changed

3 files changed

+132
-0
lines changed

guide/en/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ Views -
6363
* [Working with client scripts](views/client-scripts.md) -
6464
* [Theming](views/theming.md) -
6565
* [Template engines](views/template-engines.md) -
66+
* [View injections](views/view-injections.md) +
6667

6768

6869
Working with databases +-

guide/en/runtime/cookies.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,33 @@ private function actionProfile(\Psr\Http\Message\ServerRequestInterface $request
1717
}
1818
```
1919

20+
In addition to obtaining cookie values directly from the server request, you can also utilize the [yiisoft/request-provider](https://github.com/yiisoft/request-provider)
21+
package, which provides a more structured way to handle cookies through the `\Yiisoft\RequestProvider\RequestCookieProvider`. This approach can simplify your code and improve readability.
22+
23+
Here’s an example of how to work with cookies using the `\Yiisoft\RequestProvider\RequestCookieProvider`:
24+
25+
```php
26+
final class MyService
27+
{
28+
public function __construct(
29+
private \Yiisoft\RequestProvider\RequestCookieProvider $cookies
30+
) {}
31+
32+
public function go(): void
33+
{
34+
// Check if a specific cookie exists
35+
if ($this->cookies->has('foo')) {
36+
// Retrieve the value of the cookie
37+
$fooValue = $this->cookies->get('foo');
38+
// Do something with the cookie value
39+
}
40+
41+
// Retrieve another cookie value
42+
$barValue = $this->cookies->get('bar');
43+
// Do something with the bar cookie value
44+
}
45+
}
46+
2047
## Sending cookies
2148

2249
Since sending cookies is, in fact, sending a header but since forming the header isn't trivial, there is

guide/en/views/view-injections.md

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# View injections
2+
3+
The view injections are designed to provide a standardized way to pass parameters to the common layer
4+
5+
of views in an application. Implementing this interface allows developers to manage the data that will be available
6+
across various views, ensuring flexibility and reusability of code.
7+
8+
## Configuration
9+
10+
In config `params.php`:
11+
12+
13+
```php
14+
...
15+
'yiisoft/yii-view' => [
16+
'injections' => [
17+
Reference::to(ContentViewInjection::class),
18+
Reference::to(CsrfViewInjection::class),
19+
Reference::to(LayoutViewInjection::class),
20+
],
21+
],
22+
```
23+
24+
## New injections
25+
26+
Start by defining a class that will implement the `Yiisoft\Yii\View\Renderer\CommonParametersInjectionInterface`. This
27+
class will be responsible for providing the parameters you want to inject into your view templates and layouts.
28+
29+
```php
30+
class MyCustomParametersInjection implements Yiisoft\Yii\View\Renderer\CommonParametersInjectionInterface
31+
{
32+
// Class properties and methods will go here
33+
34+
public function __construct(UserService $userService)
35+
{
36+
$this->userService = $userService;
37+
}
38+
39+
public function getCommonParameters(): array
40+
{
41+
return [
42+
'siteName' => 'My Awesome Site',
43+
'currentYear' => date('Y'),
44+
'user' => $this->userService->getCurrentUser(),
45+
];
46+
}
47+
}
48+
```
49+
50+
Add your new Injection to `params.php`:
51+
52+
```php
53+
'yiisoft/yii-view' => [
54+
'injections' => [
55+
...,
56+
Reference::to(MyCustomParametersInjection::class),
57+
],
58+
],
59+
```
60+
61+
## Using Separate Injections for Different Layouts
62+
63+
If your application has multiple layouts, you can create separate parameter injections for each layout. This approach
64+
allows you to tailor the parameters injected into each layout according to its specific needs, enhancing the flexibility
65+
and maintainability of your application.
66+
67+
Create your custom ViewInjection for specific layout:
68+
69+
```php
70+
readonly final class CartViewInjection implements CommonParametersInjectionInterface
71+
{
72+
public function __construct(private Cart $cart)
73+
{
74+
}
75+
76+
public function getCommonParameters(): array
77+
{
78+
return [
79+
'cart' => $this->cart,
80+
];
81+
}
82+
}
83+
```
84+
85+
Add your new injection to `params.php` under specific layout name. In the following example, it is `@layout/cart`:
86+
87+
```php
88+
'yiisoft/yii-view' => [
89+
'injections' => [
90+
...,
91+
Reference::to(MyCustomParametersInjection::class),
92+
DynamicReference::to(static function (ContainerInterface $container) {
93+
$cart = $container
94+
->get(Cart::class);
95+
96+
return new LayoutSpecificInjections(
97+
'@layout/cart', // layout name for injection
98+
99+
new CartViewInjection($cart)
100+
);
101+
}),
102+
],
103+
],
104+
```

0 commit comments

Comments
 (0)