gpt4 book ai didi

zend-framework2 - zf2 : how to get Service Locator in Model

转载 作者:行者123 更新时间:2023-12-04 11:54:13 25 4
gpt4 key购买 nike

我尝试了其他帖子和示例中的一些想法,但我仍然没有解决方案。
我无法在我的模型中收到服务定位器。

我的 Module.php 在函数 getServiceConfig() 中包含以下工厂:

'Backend\Model\Beuser' => function($sm){
$serviceLocator = $sm->getServiceLocator();
return new \Backend\Model\Beuser($serviceLocator);
},

我的模型 Beuser.php 看起来像:
namespace Backend\Model;

use Zend\InputFilter\Factory as InputFactory;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;


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

class Beuser implements ServiceLocatorAwareInterface
{

public $idx_be_user;
public $be_user_email;
public $be_user_password;
public $be_user_firstname;
public $be_user_lastname;
public $be_user_phone;
public $be_user_photo;
public $be_user_shorttext;

protected $inputFilter;

protected $serviceLocator;

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

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

public function exchangeArray($data)
{

$this->idx_be_user = (!empty($data['idx_be_user'])) ? $data['idx_be_user'] : null;
$this->be_user_email = (!empty($data['be_user_email'])) ? $data['be_user_email'] : null;
$this->be_user_password = (!empty($data['be_user_password'])) ? $data['be_user_password'] : null;
$this->be_user_firstname = (!empty($data['be_user_firstname'])) ? $data['be_user_firstname'] : null;
$this->be_user_lastname = (!empty($data['be_user_lastname'])) ? $data['be_user_lastname'] : null;
$this->be_user_phone = (!empty($data['be_user_phone'])) ? $data['be_user_phone'] : null;
$this->be_user_photo = (!empty($data['be_user_photo'])) ? $data['be_user_photo'] : null;
$this->be_user_shorttext = (!empty($data['be_user_shorttext'])) ? $data['be_user_shorttext'] : null;

}

public function getArrayCopy()
{
return get_object_vars($this);
}

public function setInputFilter(InputFilterInterface $inputFilter)
{
throw new \Exception("Not used");
}

public function getInputFilter()
{

var_dump($this->getServiceLocator());
die();
if (!$this->inputFilter) {
$inputFilter = new InputFilter();
$factory = new InputFactory();

$inputFilter->add($factory->createInput(array(
'name' => 'idx_be_user',
'required' => true,
'filters' => array(
array('name' => 'Int'),
),
)));

$inputFilter->add($factory->createInput(array(
'name' => 'be_user_email',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
),
),
/*
array(
'name' => 'Db\NoRecordExists',
'options' => array(
'table' => 'users',
'field' => 'email',
// 'adapter' => $this->getServiceLocator()->get('dbAdapter'),
),
),
*/

),
)));

$inputFilter->add($factory->createInput(array(
'name' => 'be_user_password',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 6,
'max' => 100,
),
),
),
)));

$inputFilter->add($factory->createInput(array(
'name' => 'be_user_firstname',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
),
),
),
)));

$inputFilter->add($factory->createInput(array(
'name' => 'be_user_lastname',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
),
),
),
)));

$inputFilter->add($factory->createInput(array(
'name' => 'be_user_phone',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
),
),
),
)));


$inputFilter->add(
$factory->createInput(array(
'name' => 'be_user_photo',
'required' => false,
))
);


$inputFilter->add($factory->createInput(array(
'name' => 'be_user_shorttext',
'required' => false,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 5,
'max' => 255,
),
),
),
)));

$this->inputFilter = $inputFilter;
}

return $this->inputFilter;
}

}


var_dump($this->getServiceLocator());返回 NULL。这就是为什么调用 $this->getServiceLocator()->get('something') 的原因。
创建一个
PHP Fatal error: Call to a member function get()[...]
你能帮我找出问题吗?

PS:我还尝试了以下工厂:
 'Backend\Model\Beuser' => function($sm){
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$beuser = new \Backend\Model\Beuser();
$beuser->setDbAdapter($dbAdapter);
return $beuser;
},

我的模块.php:
namespace Backend;

use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use Zend\Authentication\Storage;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\Authentication\AuthenticationService;
use Zend\Authentication\Adapter\DbTable as AuthAdapter;
use Zend\Authentication\Result as Result;
use Zend\Session\Container; // We need this when using sessions

//Be User
use Backend\Model\Beuser;
use Backend\Model\BeuserTable;


class Module implements AutoloaderProviderInterface
{

public function getAutoloaderConfig()
{
return array(
'Zend\Loader\ClassMapAutoloader' => array(
__DIR__ . '/autoload_classmap.php'
),
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}

public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}

public function getServiceConfig()
{
return array(

//factories
'factories'=>array(

//Storage
'BackendStorage' => function($sm){
return new BackendStorage('backend');
},
//DB Adapter
'dbAdapter' => function($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
return $dbAdapter;
},
//nur DB
'db' => function($sm) {
$dba= $sm->get('dbAdapter');
$db = new db\Db($dba);
return $db;
},
//mache Auth Info verfuegbar
'AuthService' => function($sm) {
//$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$dbTableAuthAdapter = new AuthAdapter($sm->get('dbAdapter'),'tbl_be_user','be_user_email','be_user_password', 'MD5(?)');
//$authService = new AuthenticationService();
$authService = new AuthenticationService(new Storage\Session('Auth_Backend'));
$authService->setAdapter($dbTableAuthAdapter);
return $authService;

},
//Navigation
'NavigationFactory' => 'Backend\Service\NavigationFactory',

// Backend User
'Backend\Model\BeuserTable' => function($sm) {
$tableGateway = $sm->get('BeuserTableGateway');
$table = new BeuserTable($tableGateway);
return $table;
},
'BeuserTableGateway' => function ($sm) {
$dbAdapter = $sm->get('dbAdapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Beuser());
return new TableGateway('tbl_be_user', $dbAdapter, null, $resultSetPrototype);
},

'Backend\Model\Beuser' => function($sm){
$model = new \Backend\Model\Beuser();
$model->setServiceLocator($sm);

return $model;
},

),



);

}

public function getViewHelperConfig()
{
return array(
'invokables' => array(
'PageTitle' => 'Backend\ViewHelper\PageTitle',
),
'factories' => array(
//hasIdentity true or false
'AuthStatusFactory' => function ($serviceManager) {
// Get the service locator
$serviceLocator = $serviceManager->getServiceLocator();
// pass it to your helper
return new \Backend\ViewHelper\AuthStatusFactory($serviceLocator);
},
'BeUserName' => function ($serviceManager) {
// Get the service locator
$serviceLocator = $serviceManager->getServiceLocator();
// pass it to your helper
return new \Backend\ViewHelper\BeUserName($serviceLocator);
},
)
);
}


}

最佳答案

您通过不存在的构造函数将其传递...只需使用 setter 。 Service Manager 实现 ServiceLocatorInterface,只需将其传递给您的模型。

更改您的服务配置:

'Backend\Model\Beuser' => function($sm){
$model = new \Backend\Model\Beuser();
$model->setServiceLocator($sm);

return $model;
},

Controller 中的示例访问:
public function testAction()
{
// The service manager will then inject itself into your model
// when it is instantiated.
$myModel = $this->getServiceLocator()->get('Backend\Model\Beuser');

// now you have the Service Locator injecte4d into your model ..
//var_dump($myModel->getServiceLocator());
}

您还需要确保使用服务定位器来获取您需要创建的模型的任何实例,否则将不会注入(inject)任何内容:
'BeuserTableGateway' => function ($sm) {
$dbAdapter = $sm->get('dbAdapter');
$resultSetPrototype = new ResultSet();
// Beuser will not have the service locator injected below..
// and will return null if the object wasn't created via the service manager.
//$resultSetPrototype->setArrayObjectPrototype(clone $sm->get('Backend\Model\Beuser'));
$resultSetPrototype->setArrayObjectPrototype(new Beuser());

return new TableGateway('tbl_be_user', $dbAdapter, null, $resultSetPrototype);
},

关于zend-framework2 - zf2 : how to get Service Locator in Model,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17443125/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com