ZF2教程 - 委托器的使用

来源:互联网 发布:mac os 10.13 dmg下载 编辑:程序博客网 时间:2024/05/20 06:05

委托器包装类实例,扩展类的功能!ZF2通过委托器类、委托器工厂类,实现了服务功能的扩展。

一、委托器工厂接口参数的理解

namespace Zend\ServiceManager;interface DelegatorFactoryInterface{    public function createDelegatorWithName(                    ServiceLocatorInterface $serviceLocator, $name,                     $requestedName, $callback);}

委托器工厂的createDelegatorWithName()方法有四个参数,$serviceLocator参数是服务定位器,$name是类的标准化名,$requestedName是:

$buzzer = $serviceManager->get('buzzer');

上式get()方法中的名字!$callback是回调。这四个参数都不用自己输入,在服务管理器创建委托器工厂实例时,由服务管理器填充进去!

二、需要做的三件事情

1、构造委托器类

ZF2给出了标准的委托器类格式,按照格式构造一个委托器类。

namespace Restful\Model;use Restful\Controller\TrutController;use Zend\EventManager\EventManagerInterface;class TrutDelegator extends TrutController {  protected $realTrut;  protected $eventManager;  public function __construct(TrutController $realTrut,                              EventManagerInterface $eventManager)  {    $this->realTrut   = $realTrut;    $this->eventManager = $eventManager;  }  public function indexAction()  {    $this->eventManager->trigger('first.pre', $this);    echo $this->realTrut->indexAction();    $this->eventManager->trigger('first.post', $this);  }}

这里用一个用一个控制器类TrutController来给委托器包装。

2、构造委托器工厂类

委托器工厂类必须实现委托器工厂接口,委托器工厂接口只有一个方法,所以要实现这个方法。

namespace Restful\Model;use Zend\ServiceManager\DelegatorFactoryInterface;use Zend\ServiceManager\ServiceLocatorInterface;use Restful\Model\TrutDelegator;class TrutDelegatorFactory implements DelegatorFactoryInterface {  public function createDelegatorWithName(ServiceLocatorInterface $serviceLocator,                                          $name, $requestedName, $callback)   {    $realTrut   = call_user_func($callback);    $eventManager = $serviceLocator->get('EventManager');    $eventManager->attach('first.pre', function ()    { echo "<br>第一层包装开始!<br>"; });    $eventManager->attach('first.post', function ()    { echo "<br>第一层包装结束!<br><br>"; });    return new TrutDelegator($realTrut, $eventManager);    }}

使用委托器工厂的理由是需要包装的类,难以实例化,比如说注册为服务的类。从服务管理器却容易获得。

委托器可以多次包装,对于已经包装的类实例,可以再次包装,形成层层包裹的样式。为了演示再创建一个委托器工厂类。

namespace Restful\Model;use Zend\ServiceManager\DelegatorFactoryInterface;use Zend\ServiceManager\ServiceLocatorInterface;class OtherDelegatorFactory implements DelegatorFactoryInterface {  public function createDelegatorWithName(ServiceLocatorInterface $serviceLocator,                                           $name, $requestedName, $callback)   {    $realTrut   = call_user_func($callback);    $eventManager = $serviceLocator->get('EventManager');    $eventManager->attach('first.pre', function ()    { echo "第二层包装开始!<br>"; });    $eventManager->attach('first.post', function ()    { echo "第二层包装结<br>"; });    return new TrutDelegator($realTrut, $eventManager);  }}

3、注册服务

委托器工厂是一个服务必须注册,委托器必须关联类实例和委托器工厂实例,可以通过addDelegator()方法关联,也可以在服务管理器键下配置。现在采用在module.confug.php中配置。

'service_manager'=>array(  'invokables' => array(    //文档中的示例    'buzzer'                   => 'Restful\Model\Buzzer',    'buzzer-delegator-factory' => 'Restful\Model\BuzzerDelegatorFactory',    'trut'                   => 'Restful\Controller\TrutController',    'trut-delegator-factory' => 'Restful\Model\TrutDelegatorFactory',    'other-delegator-factory' => 'Restful\Model\OtherDelegatorFactory',  ),  'delegators' => array(     //文档中的示例     'buzzer' => array(        'buzzer-delegator-factory'        //最后在此加入更多的委托器     ),    'trut' => array(       'trut-delegator-factory',       'other-delegator-factory'       //最后在此加入更多的委托器    ),  ),),

在配置中也加入了文档示例的配置,在层层包装时作一下对比。

三、使用委托器

在控制器的delegator动作中使用委托器。

public function delegatorAction() {    $buzzer=$this->getServiceLocator()->get('buzzer');    echo "<h3>BuzzerDelegator演示:</h3>";    $buzzer->buzz();    echo "<h3>TrutDelegator演示:</h3>";    $trut=$this->getServiceLocator()->get('trut');    $trut->indexAction();    exit();}

在浏览器地址中输入http://localhost/test/public/rest/rest/delegator,出现了演示的结果。

delegator

委托器服务工厂英文文档已经翻译为中文,此教程结合示例讲解,所以也是示例,因此中文文档、教程、示例都已经具备,奉献给喜欢本站的人们!

四、结束

如果您对验证的中文错误消息、单选按钮的处理、多选按钮的处理、RESTful、控制台等等感兴趣,请继续关注。

http://zendframework2.duapp.com/zf2studies/delegator.html

0 0
原创粉丝点击