Skip to content

Commit

Permalink
Update view implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
alexgarrettsmith committed Jan 8, 2020
1 parent d52de41 commit 6884248
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 22 deletions.
43 changes: 24 additions & 19 deletions bootstrap/app.php
Original file line number Diff line number Diff line change
@@ -1,43 +1,48 @@
<?php

use Dotenv\Dotenv;
use Slim\Views\Twig;
use Slim\Factory\AppFactory;
use Slim\Views\TwigExtension;
use Slim\Psr7\Factory\UriFactory;
use Dotenv\Exception\InvalidPathException;

require_once __DIR__ . '/../vendor/autoload.php';

try {
(new Dotenv\Dotenv(__DIR__ . '/../'))->load();
} catch (Dotenv\Exception\InvalidPathException $e) {
(new Dotenv(__DIR__ . '/../'))->load();
} catch (InvalidPathException $e) {
//
}

$container = new DI\Container();

Slim\Factory\AppFactory::setContainer($container);
AppFactory::setContainer($container);

$app = Slim\Factory\AppFactory::create();
$app = AppFactory::create();

$container->set('settings', function () {
return [
'displayErrorDetails' => getenv('APP_DEBUG') === 'true',

'app' => [
'name' => getenv('APP_NAME')
],

'views' => [
'cache' => getenv('VIEW_CACHE_DISABLED') === 'true' ? false : __DIR__ . '/../storage/views'
]
];
});

$twig = new Slim\Views\Twig(__DIR__ . '/../resources/views', [
'cache' => $container->get('settings')['views']['cache']
]);
$container->set('view', function ($container) use ($app) {
$twig = new Twig(__DIR__ . '/../resources/views', [
'cache' => false
]);

$twigMiddleware = new Slim\Views\TwigMiddleware(
$twig,
$container,
$app->getRouteCollector()->getRouteParser()
);
$twig->addExtension(
new TwigExtension(
$app->getRouteCollector()->getRouteParser(),
(new UriFactory)->createFromGlobals($_SERVER),
'/'
)
);

$app->add($twigMiddleware);
return $twig;
});

require_once __DIR__ . '/../routes/web.php';
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"type": "project",
"require": {
"php": ">=7.0.0",
"slim/slim": "4.3.0",
"slim/slim": "4.4.0",
"slim/twig-view": "3.0.0-alpha",
"symfony/var-dumper": "^3.2",
"vlucas/phpdotenv": "^2.4",
Expand Down
2 changes: 0 additions & 2 deletions storage/views/.gitignore

This file was deleted.

0 comments on commit 6884248

Please sign in to comment.