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

Configuration Zend framework 2:如何从模型中获取自定义配置?,configuration,model,zend-framework2,Configuration,Model,Zend Framework2,在我的应用程序中,有自定义配置,我希望将它们放入模型中 我读到一种方法,但它不能执行: 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\

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

我读到一种方法,但它不能执行:

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');

但是结果=
NULL


谁知道我做错了什么?

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

关于:$this->getServiceLocator()->get('Configuration');如何创建BaseModel类的实例?如果不是从应用程序的预配置服务管理器中提取服务定位器,服务定位器将不会自动注入到对象中。
$config = $this->getServiceLocator();