Zend Expressive,从 Php Plates 模板获取服务
Zend Expressive, get service from Php Plates template
我使用Zend Expressive framework via default ZE skeleton app with Zend ServiceManager as DIC and Plates作为模板引擎。
假设我有 index.phtml
模板。我想得到一些服务,这会倾销我的资产,比如:
<?= $this->getContainer()->get('my service class')->dumpAssets() ?>
服务已通过工厂注册并可在应用程序中访问:
<? $container->get('my service class') ?>
如何将外部服务实例或其结果传递给模板?
我想出了如何通过 extensions 从模板引擎访问容器。不清楚 MVC-ly,但是...
首先,将 plates 配置添加到 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');
?>
将整个服务容器注入模板(或除工厂之外的任何其他 class)是非常糟糕的做法。更好的方法是编写一个扩展来转储资产。
分机 class:
<?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();
?>
我使用Zend Expressive framework via default ZE skeleton app with Zend ServiceManager as DIC and Plates作为模板引擎。
假设我有 index.phtml
模板。我想得到一些服务,这会倾销我的资产,比如:
<?= $this->getContainer()->get('my service class')->dumpAssets() ?>
服务已通过工厂注册并可在应用程序中访问:
<? $container->get('my service class') ?>
如何将外部服务实例或其结果传递给模板?
我想出了如何通过 extensions 从模板引擎访问容器。不清楚 MVC-ly,但是...
首先,将 plates 配置添加到 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');
?>
将整个服务容器注入模板(或除工厂之外的任何其他 class)是非常糟糕的做法。更好的方法是编写一个扩展来转储资产。
分机 class:
<?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();
?>