Skip to content

Commit

Permalink
Add ApplicationInjection.
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Aug 16, 2020
1 parent c1259d2 commit a3aa9f3
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 39 deletions.
4 changes: 2 additions & 2 deletions config/params.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

use App\Command;
use App\ViewRenderer\UserInjection;
use App\ViewRenderer\ApplicationInjection;
use Cycle\Schema\Generator;
use Yiisoft\Factory\Definitions\Reference;

Expand Down Expand Up @@ -92,7 +92,7 @@
// DynamicReference::to(function (ContainerInterface $container) {
// return $container->get(CsrfInjection::class)->withParameter('mycsrf');
// }),
Reference::to(UserInjection::class),
Reference::to(ApplicationInjection::class),
],
],
];
32 changes: 32 additions & 0 deletions src/ViewRenderer/ApplicationInjection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace App\ViewRenderer;

use Yiisoft\Router\UrlMatcherInterface;
use Yiisoft\Yii\Web\User\User;

class ApplicationInjection extends AbstractInjection
{

private User $user;
private UrlMatcherInterface $urlMatcher;

public function __construct(
User $user,
UrlMatcherInterface $urlMatcher
) {
$this->user = $user;
$this->urlMatcher = $urlMatcher;
}

public function getLayoutParams(): array
{
return [
'user' => $this->user->getIdentity(),
'currentUrl' => (string)$this->urlMatcher->getLastMatchedRequest()->getUri(),
'brandLabel' => 'Yii Demo',
];
}
}
33 changes: 0 additions & 33 deletions src/ViewRenderer/UserInjection.php

This file was deleted.

10 changes: 6 additions & 4 deletions views/layout/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
* @var \Yiisoft\Router\UrlGeneratorInterface $urlGenerator
* @var Yiisoft\Router\UrlMatcherInterface $urlMatcher
* @var \Yiisoft\View\WebView $this
* @var \App\Entity\User $user
* @var \Yiisoft\Assets\AssetManager $assetManager
* @var string $content
*
* @see \App\ViewRenderer\ApplicationInjection
* @var \App\Entity\User $user
* @var string $currentUrl
* @var string $brandLabel
*/

$currentUrl = (string) $urlMatcher->getLastMatchedRequest()->getUri();

$assetManager->register([
AppAsset::class
]);
Expand All @@ -38,7 +40,7 @@
$this->beginBody();

echo NavBar::begin()
->brandLabel('Yii Demo')
->brandLabel($brandLabel)
->brandUrl($urlGenerator->generate('site/index'))
->options(['class' => 'navbar navbar-light bg-light navbar-expand-sm text-white'])
->start();
Expand Down

0 comments on commit a3aa9f3

Please sign in to comment.