zf2中使用service manager的使用方法
博客专区 > webphp 的博客 > 博客详情
zf2中使用service manager的使用方法
webphp 发表于3个月前
zf2中使用service manager的使用方法
  • 发表于 3个月前
  • 阅读 0
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 十分钟定制你的第一个小程序>>>   

方法1:

在控制层中的controller文件中
//file:/module/User/src/User/Controller/IndexController.php
namespace /User/Controller 
use Zend\Helper\Help;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\ServiceManager\ServiceManager;
use Zend\Stdlib\DispatchableInterface;
class indexController extends AbstractActionController implements DispatchableInterface,ServiceLocatorAwareInterface{
    public funciton indexAction(){
        $serviceManger = new ServiceManager();
        //setService服务注册(Service registration)。ServiceManager::setService 允许你注册一个对象作为服务:
        $serviceManger->setService('helper', new Help());
        $serviceManger->get('helper')->dump( array('title'=>'zf2', 'likes'=>500) );
        //延迟加载的服务对象(Lazy-loaded service objects)
        $serviceManger->setInvokableClass('myhelper', 'Zend\Helper\Help');
        $serviceManger->get('myhelper')->dump( array('title'=>'servicemanager','likes'=>900) );
    }
}

方法2:

    在mondule.php文件中配置,然后在controller文件中调用
file :/module/User/Module.php
namespace User;
use Zend\Helper\Help
class Module{ 
    public function getServiceConfig(){
        return array(
            'factories' => array(
                'User\Model\AlbumTable' => function($sm){
                    $tableGateway = $sm->get('AlbumTableGateway');
                    $table = new AlbumTable($tableGateway);
                    return $table;
                },
                'AlbumTableGateway' => function($sm){
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $resultSetPrototype = new ResultSet();
                    $resultSetPrototype->setArrayObjectPrototype( new Album() );
                    return new TableGateway('album', $dbAdapter, null, $resultSetPrototype);
                },
            ),
            //延时加载,根据不同的类进行不同选择,在此invokables和service可以任选一个
            'invokables' => array(
                'MyHelper' => 'Zend\Helper\Help',
            ),
            //添加服务
            //'services' => array(
            //    'MyHelper' => new Help(),
            // ),
        );
    }
}
    格式为:
public function getServiceConfig()
    {
        return array(
            'abstract_factories' => array(),
            'aliases' => array(),
            'factories' => array(),
            'invokables' => array(),
            'services' => array(),
            'shared' => array(),
        );
    }
    在Module.php文件中配置完成以后,在controller文件中使用如下
use Zend\Mvc\Controller\AbstractActionController;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceManager;
use Zend\Stdlib\DispatchableInterface;
use Zend\View\Model\ViewModel;
class IndexController extends AbstractActionController implements DispatchableInterface,ServiceLocatorAwareInterface {
    public function indexAction(){
        $this->getServiceLocator()->get('MyHelper')->dump(array('title'=>'baiping','likes'=>1000));
    }
}
如果想在model层中使用service manager中的配置向下:
//file: module/User/src/User/Model/AlbumTable.php

namespace User\Model;
use Zend\Db\TableGateway\TableGateway;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
class AlbumTable implements ServiceLocatorAwareInterface {
    protected $tableGateway;
    protected $services;
    public function __construct(TableGateway $tableGateway){
        $this->tableGateway = $tableGateway;
    }

    //ServiceLocatorInterface
    public function setServiceLocator(ServiceLocatorInterface $serviceLocator) {
        $this->services = $serviceLocator;
    }
    public function getServiceLocator() {
        return $this->serviceLocator;
    }
    public function fetchAll(){
        //此处表示可以使用Helper公共库中dump方法
        $this->services->get('MyHelper')->dump(array('title'=>'xinsight', 'likes'=>'109'));
        $resultSet = $this->tableGateway->select();
        return $resultSet;
    }
    public function getAlbum($id){
        $id = (int) $id;
        $rowset = $this->tableGateway->select( array('id'=>$id) );
        $row = $rowset->current();
        if ( !$row ){
            throw new \Exception("Could not find row $id");
        }
        return $row;
    }
    public function saveAlbum(Album $album){
        $data = array(
            'artist' => $album->artist,
            'title'  => $album->title,
        );
        $id = (int) $album->id;
        if ( $id == 0 ){
            $this->tableGateway->insert($data);
        }else{
            if ( $this->getAlbum($id) ){
                $this->tableGateway->update($data, array('id'=>$id));
            }else{
                throw new \Exception('Album id does not exist');
            }
        }
    }
    public function deleteAlbum($id){
        $this->tableGateway->delete( array('id'=>$id) );
    }
}
 
标签: php zend framework
共有 人打赏支持
粉丝 2
博文 88
码字总数 90487
×
webphp
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: