繁体   English   中英

Zend 框架 2:如何从模型中获取自定义配置?

[英]Zend framework 2: How to get custom config from model?

在我的应用程序中,有自定义配置,我想将它们放入模型中。

我读了一种方式,但它不能执行:

namespace Core\Model;

use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\TableGateway\Feature\FeatureSet;
use Zend\Db\TableGateway\Feature\GlobalAdapterFeature;
use Zend\Db\Sql\Delete,
    Zend\Db\Sql\Insert,
    Zend\Db\Sql\Update,
    Zend\Db\Sql\Select;

use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class BaseModel extends AbstractTableGateway implements ServiceLocatorAwareInterface
{

    protected $serviceLocator;

    public function setServiceLocator(ServiceLocatorInterface $serviceLocator) {
        $this->serviceLocator = $serviceLocator;
    }

    public function getServiceLocator() {
        return $this->serviceLocator;
    }

    public function __construct()
    {
        $this->featureSet = new FeatureSet();
        $this->featureSet->addFeature(new GlobalAdapterFeature());
        $this->initialize();
    }
}

在我规定的模型中

$config = $this->getServiceLocator()->get('config');

或者

$config = $this->getServiceLocator();

但结果 = NULL

谁能告诉我我做错了什么?

您必须创建使用 ServiceManager 扩展BaseModel的类的实例。 如果使用new ,则必须自己设置 ServiceManager 。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM