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