0

我通过默认的 ZE 骨架应用程序使用Zend Expressive 框架,其中 Zend ServiceManager 作为 DIC,Plates作为模板引擎。

假设我有index.phtml模板。我想获得一些服务,这会倾销我的资产,比如:

<?= $this->getContainer()->get('my service class')->dumpAssets() ?>

服务通过工厂注册并可以在应用程序中访问:

<? $container->get('my service class') ?>

如何将外部服务实例或其结果传递给模板?

4

2 回答 2

1

将整个服务容器注入模板(或工厂以外的任何其他类)是非常糟糕的做法。更好的方法是编写一个扩展来转储资产。

扩展类:

<?php

namespace App\Container;

use League\Plates\Engine;
use League\Plates\Extension\ExtensionInterface;
use App\Service\AssetsService;

class DumpAssetsExtension implements ExtensionInterface
{
    public $assetsService;

    /**
     * AssetsExtension constructor.
     * @param $container
     */
    public function __construct(AssetsService $assetsService)
    {
        $this->assetsService = $assetsService;
    }

    public function register(Engine $engine)
    {
        $engine->registerFunction('dumpAssets', [$this, 'dumpAssets']);
    }

    public function dumpAssets()
    {
        return $this->assetsService->dumpAssets();
    }
}

工厂:

<?php

namespace App\Container;

use Interop\Container\ContainerInterface;

class DumpAssetsFactory
{
    public function __invoke(ContainerInterface $container)
    {
        $assetsService = $container->get(App\Service\AssetsService::class);

        return new PlatesExtension($assetsService);
    }
}

配置:

<?php

return [
    // ...
   'factories' => [
       App\Container\DumpAssetsExtension::class => App\Container\DumpAssetsFactory::class,
   ]
];

在您的模板中:

<?php
    $service = $this->dumpAssets();
?>
于 2016-05-19T11:09:55.640 回答
0

我想出了如何通过扩展从模板引擎访问容器。目前尚不清楚 MVC-ly,但是...

首先,将板配置添加到config/autoload/templates.global

return [
    // some othe settings    
    'plates' => [
        'extensions' => [
            App\Container\PlatesExtension::class,
        ],
    ],
],

其次,App\Container\PlatesExtension.php用代码创建:

<?php

namespace App\Container;

use League\Plates\Engine;
use League\Plates\Extension\ExtensionInterface;

class PlatesExtension implements ExtensionInterface
{
    public $container;

    /**
     * AssetsExtension constructor.
     * @param $container
     */
    public function __construct($container)
    {
        $this->container = $container;
    }

    public function register(Engine $engine)
    {
        $engine->registerFunction('container', [$this, 'getContainer']);
    }

    public function getContainer()
    {
        return $this->container;
    }
}

第三,创建工厂App\Container\PlatesExtensionFactory.php将容器注入到板块扩展中:

<?php

namespace App\Container;

use Interop\Container\ContainerInterface;

class PlatesExtensionFactory
{
    public function __invoke(ContainerInterface $container)
    {
        return new PlatesExtension($container);
    }
}

接下来,在 ServiceManager ( ) 中注册车牌扩展config/dependencies.global.php

return [
    // some other settings
   'factories' => [
       App\Container\PlatesExtension::class => App\Container\PlatesExtensionFactory::class,
   ]
];

最后,从 Plates 模板中获取容器和所需的服务:

<?
    $service = $this->container()->get('my service class');
?>
于 2016-04-10T10:29:21.347 回答