【发布时间】:2017-08-15 22:24:17
【问题描述】:
早上好,我一直在学习使用框架(Zend 框架)进行编程。 在我过去的经验中,我使用的是骨架应用程序 v.2.5。话虽如此,我过去开发的所有模块都是围绕 ServiceManager 中的 servicelocator() 工作的。 有没有办法在 zend 框架 3 中安装 ServiceManager(具有服务定位器功能)?
如果没有,您能否给我一个合适的方法来解决 servicelocator?
感谢您的关注,祝您有美好的一天:)
*/ 已更新 - 以小模块为例。 作为示例,我将向您展示我在 2.5 中使用的登录身份验证模块:
我的模块.php
<?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;
},
),
);
}
}
我的 AuthController:
<?php
//module/SanAuth/src/SanAuth/Controller/AuthController.php
namespace SanAuth\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\Form\Annotation\AnnotationBuilder;
use Zend\View\Model\ViewModel;
use SanAuth\Model\User;
class AuthController extends AbstractActionController
{
protected $form;
protected $storage;
protected $authservice;
public function getAuthService()
{
if (! $this->authservice) {
$this->authservice = $this->getServiceLocator()
->get('AuthService');
}
return $this->authservice;
}
public function getSessionStorage()
{
if (! $this->storage) {
$this->storage = $this->getServiceLocator()
->get('SanAuth\Model\MyAuthStorage');
}
return $this->storage;
}
public function getForm()
{
if (! $this->form) {
$user = new User();
$builder = new AnnotationBuilder();
$this->form = $builder->createForm($user);
}
$this->form->setLabel('Entrar')
->setAttribute('class', 'comment_form')
->setAttribute('style', 'width: 100px;');
return $this->form;
}
public function loginAction()
{
//if already login, redirect to success page
if ($this->getAuthService()->hasIdentity()){
return $this->redirect()->toRoute('success');
}
$form = $this->getForm();
return array(
'form' => $form,
'messages' => $this->flashmessenger()->getMessages()
);
}
public function authenticateAction()
{
$form = $this->getForm();
$redirect = 'login';
$request = $this->getRequest();
if ($request->isPost()){
$form->setData($request->getPost());
if ($form->isValid()){
//check authentication...
$this->getAuthService()->getAdapter()
->setIdentity($request->getPost('username'))
->setCredential($request->getPost('password'));
$result = $this->getAuthService()->authenticate();
foreach($result->getMessages() as $message)
{
//save message temporary into flashmessenger
$this->flashmessenger()->addMessage($message);
}
if ($result->isValid()) {
$redirect = 'success';
//check if it has rememberMe :
if ($request->getPost('rememberme') == 1 ) {
$this->getSessionStorage()
->setRememberMe(1);
//set storage again
$this->getAuthService()->setStorage($this->getSessionStorage());
}
$this->getAuthService()->getStorage()->write($request->getPost('username'));
}
}
}
return $this->redirect()->toRoute($redirect);
}
public function logoutAction()
{
$this->getSessionStorage()->forgetMe();
$this->getAuthService()->clearIdentity();
$this->flashmessenger()->addMessage("You've been logged out");
return $this->redirect()->toRoute('login');
}
}
我的成功控制器:
<?php
//module/SanAuth/src/SanAuth/Controller/SuccessController.php
namespace SanAuth\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class SuccessController extends AbstractActionController
{
public function indexAction()
{
if (! $this->getServiceLocator()
->get('AuthService')->hasIdentity()){
return $this->redirect()->toRoute('login');
}
return new ViewModel();
}
}
我的用户.php:
<?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;
}
还有 MyAuthStorage.php:
<?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();
}
}
【问题讨论】:
-
“如何安装 servicelocator”这个问题很好。 “告诉我如何编写解决方法”这个问题太宽泛了。
标签: php zend-framework3 service-locator