如何在 slimframework v4 中添加树枝视图

mk9*_*990 7 php slim slim-4

我正在尝试在 slim v4 中添加树枝视图

在 slim v3 中,我们在容器中添加了 twig-view

$container['view'] = function ($c) {
    $view = new \Slim\Views\Twig('path/to/templates', [
        'cache' => 'path/to/cache'
    ]);

    // Instantiate and add Slim specific extension
    $router = $c->get('router');
    $uri = \Slim\Http\Uri::createFromEnvironment(new \Slim\Http\Environment($_SERVER));
    $view->addExtension(new \Slim\Views\TwigExtension($router, $uri));

    return $view;
};
Run Code Online (Sandbox Code Playgroud)

但我不能在 slim v4 中添加这样的树枝

Nim*_*ima 14

更新 Twig-View已达到稳定版本,并且文档已更新以解决 Slim 4 集成问题。
如果您仍在使用不稳定版本的 Twig-View,请考虑升级。

首先,您需要将 Twig-View 包添加到您的项目中:

composer require slim/twig-view
Run Code Online (Sandbox Code Playgroud)

并假设以下目录结构:

composer.json
cache/
public/
  |--index.php
templates/
  |--hello.twig
vendor/
  |--autoload.php
Run Code Online (Sandbox Code Playgroud)

以下是两个工作示例:

如果您使用容器(根据Slim 4 docs是可选的),您可以将 Tiwg 创建定义添加到容器并在需要时使用它。(php-di/php-di在这个例子中使用,但你可以使用任何 PSR 兼容的依赖容器。

index.php,使用容器

<?php

use DI\Container;
use Slim\Factory\AppFactory;
use Slim\Views\Twig;
use Slim\Views\TwigMiddleware;

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

// Create Container
$container = new Container();
AppFactory::setContainer($container);

// Set view in Container
$container->set('view', function() {
    return Twig::create(__DIR__ . '/../templates',
        ['cache' => __DIR__ . '/../cache']);
});

// Create App
$app = AppFactory::create();

// Add Twig-View Middleware
$app->add(TwigMiddleware::createFromContainer($app));

// Example route
$app->get('/hello/{name}', function ($request, $response, $args) {
    return $this->get('view')->render($response, 'hello.twig', [
        'name' => $args['name']
    ]);
});

// Run the app
$app->run();
Run Code Online (Sandbox Code Playgroud)

您也可以跳过容器创建,但在这种情况下,您需要在尝试渲染模板之前创建 Twig 实例。

index.php,没有容器

<?php

use Slim\Factory\AppFactory;
use Slim\Views\Twig;
use Slim\Views\TwigMiddleware;

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

// Create App
$app = AppFactory::create();

// Create Twig
$twig = Twig::create(__DIR__ . '/../templates',
    ['cache' => __DIR__ . '/../cache']);

// Add Twig-View Middleware
$app->add(TwigMiddleware::create($app, $twig));

// Example route
// Please note how $view is created from the request
$app->get('/hello/{name}', function ($request, $response, $args) {
    $view = Twig::fromRequest($request);
    return $view->render($response, 'hello.twig', [
        'name' => $args['name']
    ]);
});

// Run the app
$app->run();
Run Code Online (Sandbox Code Playgroud)

你好。树枝

Hello {{ name }}
Run Code Online (Sandbox Code Playgroud)

现在尝试/hello/slim4在浏览器中访问,输出将是:

你好slim4


Con*_*eeC 5

SlimTwigView 处于 3.0.0 测试版(至少截至 2019 年 10 月 12 日),有些事情已经发生了变化。我看过的少数在线教程以及官方文档不再有效。

TwigMiddleware 不再将 $container 的实例作为参数,因此您必须先手动将 Twig 放在 Container 上,例如:

$container->set('view', function() {
    // Of course put correct path to your views here
    return new Twig('../views', ['cache' => false]);
});
Run Code Online (Sandbox Code Playgroud)

然后,您可以使用类的新 createFromContainer 方法将 TwigMiddleware 添加到您的 Slim 应用程序,如下所示:

$app->add(TwigMiddleware::createFromContainer($app));
// which is equivalent to:
// $app->add(TwigMiddleware::createFromContainer($app, 'view'));
Run Code Online (Sandbox Code Playgroud)

此时,您可以像这样渲染 Twig 视图:

$app->get('/', function (Request $request, Response $response, $args) {
    return $this->get('view')->render($response, 'home.twig');
});
Run Code Online (Sandbox Code Playgroud)

使用 Slim 特定中间件时,您现在可以访问其他 Twig 扩展:

url_for
full_url_for
is_current_url
当前网址
获取_uri