Php Zend Framework 3中的GetServiceLocator
早上好,我一直在学习使用框架(Zend框架)编程。 在我过去的经验中,我使用的是一个骨架应用程序v.2.5。尽管如此,我过去开发的所有模块都是围绕ServiceManager中的servicelocator()工作的。 有没有办法在zend framework 3中安装ServiceManager(带有servicelocator功能) 如果没有,你能给我一个propor方法来解决servicelocator吗 谢谢你的鼓励,祝你有一个美好的一天:) */更新-以小模块为例。 作为示例,我将向您展示我在2.5中使用的登录身份验证模块: mymodule.phpPhp Zend Framework 3中的GetServiceLocator,php,zend-framework3,service-locator,Php,Zend Framework3,Service Locator,早上好,我一直在学习使用框架(Zend框架)编程。 在我过去的经验中,我使用的是一个骨架应用程序v.2.5。尽管如此,我过去开发的所有模块都是围绕ServiceManager中的servicelocator()工作的。 有没有办法在zend framework 3中安装ServiceManager(带有servicelocator功能) 如果没有,你能给我一个propor方法来解决servicelocator吗 谢谢你的鼓励,祝你有一个美好的一天:) */更新-以小模块为例。 作为示例,我将向您
<?php
namespace SanAuth;
use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use Zend\ModuleManager\Feature\ConfigProviderInterface;
use Zend\Authentication\Storage;
use Zend\Authentication\AuthenticationService;
use Zend\Authentication\Adapter\DbTable as DbTableAuthAdapter;
class Module implements AutoloaderProviderInterface
{
public function getAutoloaderConfig()
{
return array(
'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'=>array(
'SanAuth\Model\MyAuthStorage' => function($sm){
return new \SanAuth\Model\MyAuthStorage('zf_tutorial');
},
'AuthService' => function($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$dbTableAuthAdapter = new DbTableAuthAdapter($dbAdapter,
'users','user_name','pass_word', 'MD5(?)');
$authService = new AuthenticationService();
$authService->setAdapter($dbTableAuthAdapter);
$authService->setStorage($sm->get('SanAuth\Model\MyAuthStorage'));
return $authService;
},
),
);
}
}
ZF3中不再有服务定位器,因为它被视为反模式
正确的方法是在服务管理器中使用工厂,并将特定的依赖项传递到类中
如果您有任何代码可以显示,我很乐意进一步帮助您
根据提供的示例进行编辑
首先,使用composer进行自动加载,而不是使用旧的Zend内容。在Module.php中,删除自动加载。还需要删除该文件
添加一个composer.json文件并设置PSR-0或PSR-4自动加载(如果不知道如何进行,请询问)
回到模块类,您还需要更改service manager配置。我将匿名函数保留在这里,但您应该使用适当的类
<?php
namespace SanAuth;
use Zend\Authentication\Storage;
use Zend\Authentication\AuthenticationService;
use Zend\Authentication\Adapter\DbTable as DbTableAuthAdapter;
final class Module
{
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
public function getServiceConfig()
{
return [
'factories'=> [
\SanAuth\Model\MyAuthStorage::class => function($container){
return new \SanAuth\Model\MyAuthStorage('zf_tutorial');
},
'AuthService' => function($container) {
$dbAdapter = $sm->get(\Zend\Db\Adapter\Adapter::class);
$dbTableAuthAdapter = new DbTableAuthAdapter($dbAdapter, 'users','user_name','pass_word', 'MD5(?)');
$authService = new AuthenticationService();
$authService->setAdapter($dbTableAuthAdapter);
$authService->setStorage($container->get(SanAuth\Model\MyAuthStorage::class));
return $authService;
},
),
);
}
}
ZF3中不再有服务定位器,因为它被视为反模式
正确的方法是在服务管理器中使用工厂,并将特定的依赖项传递到类中
如果您有任何代码可以显示,我很乐意进一步帮助您
根据提供的示例进行编辑
首先,使用composer进行自动加载,而不是使用旧的Zend内容。在Module.php中,删除自动加载。还需要删除该文件
添加一个composer.json文件并设置PSR-0或PSR-4自动加载(如果不知道如何进行,请询问)
回到模块类,您还需要更改service manager配置。我将匿名函数保留在这里,但您应该使用适当的类
<?php
namespace SanAuth;
use Zend\Authentication\Storage;
use Zend\Authentication\AuthenticationService;
use Zend\Authentication\Adapter\DbTable as DbTableAuthAdapter;
final class Module
{
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
public function getServiceConfig()
{
return [
'factories'=> [
\SanAuth\Model\MyAuthStorage::class => function($container){
return new \SanAuth\Model\MyAuthStorage('zf_tutorial');
},
'AuthService' => function($container) {
$dbAdapter = $sm->get(\Zend\Db\Adapter\Adapter::class);
$dbTableAuthAdapter = new DbTableAuthAdapter($dbAdapter, 'users','user_name','pass_word', 'MD5(?)');
$authService = new AuthenticationService();
$authService->setAdapter($dbTableAuthAdapter);
$authService->setStorage($container->get(SanAuth\Model\MyAuthStorage::class));
return $authService;
},
),
);
}
}
非常感谢您的回复,我用登录验证模块更新了这个问题。如果你能带我完成我需要在这个模块中实现的更改(以便在版本3中工作),我现在会非常感激。看看Github上的代码,库已经更新了。非常感谢您的回复,我用一个登录验证模块更新了这个问题。如果你能带我完成我需要在这个模块中实现的更改(以便在版本3中工作),我现在会非常感激。看看Github上的代码,库已经更新了。“如何安装servicelocator”这个问题很好。“演示如何编写变通方法”的问题太广泛了。“如何安装servicelocator”的问题很好。“告诉我如何编写变通方法”这个问题太宽泛了。
<?php
namespace SanAuth\Model;
use Zend\Form\Annotation;
/**
* @Annotation\Hydrator("Zend\Stdlib\Hydrator\ObjectProperty")
* @Annotation\Name("User")
*/
class User
{
/**
* @Annotation\Type("Zend\Form\Element\Text")
* @Annotation\Required({"required":"true" })
* @Annotation\Filter({"name":"StripTags"})
* @Annotation\Options({"label":"Utilizador: "})
*/
public $username;
/**
* @Annotation\Type("Zend\Form\Element\Password")
* @Annotation\Required({"required":"true" })
* @Annotation\Filter({"name":"StripTags"})
* @Annotation\Options({"label":"Password: "})
*/
public $password;
/**
* @Annotation\Type("Zend\Form\Element\Checkbox")
* @Annotation\Options({"label":"Lembrar "})
*/
public $rememberme;
/**
* @Annotation\Type("Zend\Form\Element\Submit")
* @Annotation\Attributes({"value":"Entrar"})
*/
public $submit;
}
<?php
namespace SanAuth\Model;
use Zend\Authentication\Storage;
class MyAuthStorage extends Storage\Session
{
public function setRememberMe($rememberMe = 0, $time = 1209600)
{
if ($rememberMe == 1) {
$this->session->getManager()->rememberMe($time);
}
}
public function forgetMe()
{
$this->session->getManager()->forgetMe();
}
}
<?php
namespace SanAuth;
use Zend\Authentication\Storage;
use Zend\Authentication\AuthenticationService;
use Zend\Authentication\Adapter\DbTable as DbTableAuthAdapter;
final class Module
{
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
public function getServiceConfig()
{
return [
'factories'=> [
\SanAuth\Model\MyAuthStorage::class => function($container){
return new \SanAuth\Model\MyAuthStorage('zf_tutorial');
},
'AuthService' => function($container) {
$dbAdapter = $sm->get(\Zend\Db\Adapter\Adapter::class);
$dbTableAuthAdapter = new DbTableAuthAdapter($dbAdapter, 'users','user_name','pass_word', 'MD5(?)');
$authService = new AuthenticationService();
$authService->setAdapter($dbTableAuthAdapter);
$authService->setStorage($container->get(SanAuth\Model\MyAuthStorage::class));
return $authService;
},
),
);
}
}
<?php
namespace SanAuth\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Authentication\AuthenticationService;
final class SuccessController extends AbstractActionController
{
private $authenticationService;
public function __construct(AuthenticationService $authenticationService)
{
$this->authenticationService = $authenticationService;
}
public function indexAction()
{
if (! $this->authenticationService->hasIdentity()){
return $this->redirect()->toRoute('login');
}
return new ViewModel();
}
}