1. Prepare a Login Form with this entity.
//module/SanAuth/src/SanAuth/Model/User.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":"Username:"})
*/
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":"Remember Me ?:"})
*/
public $rememberme;
/**
* @Annotation\Type("Zend\Form\Element\Submit")
* @Annotation\Attributes({"value":"Submit"})
*/
public $submit;
}
Create custom Auth Storage that extends Zend\Authentication\Storage.
//module/SanAuth/src/SanAuth/Model/MyAuthStorage.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();
}
}
2. Register that into ServiceManager in Module class.
//module/SanAuth/Module.php
namespace SanAuth;
use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use Zend\Authentication\Storage;
use Zend\Authentication\AuthenticationService;
use Zend\Authentication\Adapter\DbTable as DbTableAuthAdapter;
class Module implements AutoloaderProviderInterface
{
public function getAutoloaderConfig(){/*common code*/}
public function getConfig(){ /*common code*/}
public function getServiceConfig()
{
return array(
'factories'=>array(
'SanAuth\Model\MyAuthStorage' => function($sm){
return new \SanAuth\Model\MyAuthStorage('zf_tutorial');
},
'AuthService' => function($sm) {
//My assumption, you've alredy set dbAdapter
//and has users table with columns : user_name and pass_word
//that password hashed with md5
$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;
},
),
);
}
}
3. Create the Auth Controller
//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);
}
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');
}
}
4. Create Success Controller
//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();
}
}
5. Register controller invokables, router, and view_manager into module.config.php
return array(
'controllers' => array(
'invokables' => array(
'SanAuth\Controller\Auth' => 'SanAuth\Controller\AuthController',
'SanAuth\Controller\Success' => 'SanAuth\Controller\SuccessController'
),
),
'router' => array(
'routes' => array(
'login' => array(
'type' => 'Literal',
'options' => array(
'route' => '/auth',
'defaults' => array(
'__NAMESPACE__' => 'SanAuth\Controller',
'controller' => 'Auth',
'action' => 'login',
),
),
'may_terminate' => true,
'child_routes' => array(
'process' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:action]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
),
),
),
),
),
'success' => array(
'type' => 'Literal',
'options' => array(
'route' => '/success',
'defaults' => array(
'__NAMESPACE__' => 'SanAuth\Controller',
'controller' => 'Success',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:action]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
),
),
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'SanAuth' => __DIR__ . '/../view',
),
),
);
6. Create a login view :
//module/SanAuth/view/san-auth/auth/login.phtml
$form = $this->form;
$form->setAttribute('action', $this->url(
'login/process',
array(
'action' => 'authenticate',
)
));
$form->prepare();
echo $this->form()->openTag($form);
echo $this->formCollection($form);
echo $this->form()->closeTag();
if ($this->messages){
echo '<ul>';
foreach($this->messages as $message)
{
echo '<li>'.$message.'</li>';
}
echo '</ul>';
}
7. The success view
<!-- //module/SanAuth/view/san-auth/success/index.phtml -->
Login Success.
<br />
<a href="<?php echo $this->url('login/process', array('action'=>'logout')); ?>">Logout</a>
--------在Module.php中调用----------
namespace SanCommons;
use Zend\Mvc\MvcEvent,
Zend\ModuleManager\Feature\AutoloaderProviderInterface,
Zend\ModuleManager\Feature\ConfigProviderInterface;
class Module implements
AutoloaderProviderInterface,
ConfigProviderInterface
{
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$eventManager->attach('route', array($this, 'AuthCheck'), 2);
}
public function AuthCheck(MvcEvent $e)
{
$application = $e->getApplication();
$sm = $application->getServiceManager();
$router = $sm->get('router');
$request = $sm->get('request');
$matchedRoute = $router->match($request);
if (null !== $matchedRoute) {
$controller = $matchedRoute->getParam('controller');
$action = $matchedRoute->getParam('action');
// check auth...
$response = $e->getResponse();
if (! $sm->get('AuthService')->hasIdentity()){
$url = $router->assemble(array(), array('name' => 'login'));
$response->setStatusCode(302);
$response->getHeaders()->addHeaderLine('Location', $url);
}
}
}
public function getAutoloaderConfig(){ /*common code */}
public function getConfig(){ /* common code */ }
}
本文介绍如何使用Zend框架创建用户登录认证模块,包括自定义表单、存储类、控制器和服务配置等关键步骤。

被折叠的 条评论
为什么被折叠?



