Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use View for index page #1

Merged
merged 3 commits into from
Jun 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"yiisoft/strings": "^3.0@dev",
"yiisoft/var-dumper": "^3.0@dev",
"yiisoft/access": "^3.0@dev",
"yiisoft/event-dispatcher": "^3.0@dev",
"yiisoft/view": "^3.0@dev",
"yiisoft/router": "^3.0@dev",
"yiisoft/router-fastroute": "^3.0@dev",
Expand Down
10 changes: 9 additions & 1 deletion config/web.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

use Nyholm\Psr7\Factory\Psr17Factory;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\ResponseFactoryInterface;
Expand All @@ -17,6 +16,8 @@
use Yiisoft\Yii\Demo\Factory\MiddlewareDispatcherFactory;
use Yiisoft\Yii\Demo\Factory\AppRouterFactory;

$basePath = dirname(__DIR__);

return [
// PSR-17 factories:
RequestFactoryInterface::class => Psr17Factory::class,
Expand All @@ -33,6 +34,13 @@
UrlGeneratorInterface::class => Reference::to(RouterInterface::class),
MiddlewareDispatcher::class => new MiddlewareDispatcherFactory(),

// event dispatcher
\Psr\EventDispatcher\ListenerProviderInterface::class => \Yiisoft\EventDispatcher\Provider\Provider::class,
\Psr\EventDispatcher\EventDispatcherInterface::class => \Yiisoft\EventDispatcher\Dispatcher::class,

// view
\Yiisoft\View\WebView::class => new \Yiisoft\Yii\Demo\Factory\ViewFactory($basePath . '/views'),

'app' => [
'name' => 'Yii Demo',
'bootstrap' => ['debug' => 'debug'],
Expand Down
6 changes: 6 additions & 0 deletions public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@

$container = new Container(require Builder::path('web'));

/* @var \yii\base\Aliases $aliases */
$aliases = $container->get(\yii\base\Aliases::class);
$aliases->set('@web', __DIR__);
$aliases->set('@base', dirname(__DIR__));
$aliases->set('@views', dirname(__DIR__) . '/views');

//Yii::setContainer($container);

$container->get('app')->run();
Expand Down
2 changes: 2 additions & 0 deletions runtime/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
29 changes: 26 additions & 3 deletions src/Controllers/SiteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,30 @@
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use yii\base\Aliases;
use Yiisoft\View\ViewContextInterface;
use Yiisoft\View\WebView;

class SiteController
class SiteController implements ViewContextInterface
{
private $responseFactory;
private $aliases;
private $view;

public function __construct(ResponseFactoryInterface $responseFactory)
public function __construct(ResponseFactoryInterface $responseFactory, Aliases $aliases, WebView $view)
{
$this->responseFactory = $responseFactory;
$this->aliases = $aliases;
$this->view = $view;
}

public function index(): ResponseInterface
{
$response = $this->responseFactory->createResponse();
$response->getBody()->write('You are at homepage.');

$output = $this->render('index');

$response->getBody()->write($output);
return $response;
}

Expand All @@ -29,4 +39,17 @@ public function testParameter(ServerRequestInterface $request): ResponseInterfac
$response->getBody()->write('You are at test with param ' . $id);
return $response;
}

private function render(string $view, array $parameters = []): string
{
return $this->view->render($view, $parameters, $this);
}

/**
* @return string the view path that may be prefixed to a relative view name.
*/
public function getViewPath(): string
{
return $this->aliases->get('@views') . '/site';
}
}
31 changes: 31 additions & 0 deletions src/Factory/ViewFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
namespace Yiisoft\Yii\Demo\Factory;

use Psr\Container\ContainerInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Log\LoggerInterface;
use Yiisoft\View\Theme;
use Yiisoft\View\WebView;

class ViewFactory
{
private $basePath;

public function __construct(string $basePath)
{
$this->basePath = $basePath;
}

public function __invoke(ContainerInterface $container)
{
$theme = $container->get(Theme::class);
$logger = $container->get(LoggerInterface::class);
$eventDispatcher = $container->get(EventDispatcherInterface::class);
return new WebView($this->basePath, $theme, $eventDispatcher, $logger);
}

public static function __set_state(array $state): self
{
return new self($state['basePath']);
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions views/site/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello, world!