【发布时间】:2015-01-20 08:30:02
【问题描述】:
让我们从出了什么问题开始:我有一个与“路由”事件耦合的 acl 侦听器,它起初看起来工作正常,但一段时间后会引发一些错误。这些是错误:
致命错误:未捕获的异常 带有消息“会话”的“Zend\Session\Exception\RuntimeException” 验证失败'...
Zend\ServiceManager\Exception\ServiceNotCreatedException:一个异常 在创建“authService”时提出;没有返回实例...
Zend\ServiceManager\Exception\ServiceNotCreatedException:一个异常 在创建“MyAclListener”时提出;没有返回实例...
监听器设置如下:
class MyAclListener implements ListenerAggregateInterface
{
/** @var AuthenticationServiceInterface */
protected $_authService;
function __construct (
AuthenticationServiceInterface $authService ,
)
{
$this->_authService = $authService;
}
public function attach ( EventManagerInterface $events )
{
$this->listeners[ ] = $events->attach( 'route' , array( $this , 'checkAcl' ) );
}
public function checkAcl( MvcEvent $e ) {
\\\Do Something for which the authService is needed
}
然后,在 module.config.php 我有
$config = array(
'service_manager' => array(
'factories' => array(
'MyAclListener' => 'MyAcl\Factory\MyAclListenerFactory'
) ,
) ,
'listeners' => array(
'MyAclListener'
) ,
);
监听器的工厂:
<?php
namespace MyAcl\Factory;
use MyAcl\Listener\MyAclListener;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
class AclListenerFactory implements FactoryInterface
{
public function createService(ServiceLocatorInterface $serviceLocator)
{
// Finally create the service
return new MyAclListener(
$serviceLocator->get('authService') ,
);
}
}
Session 在 Module.php 中设置
public function onBootstrap ( MvcEvent $e )
{
$serviceManager = $e->getApplication()->getServiceManager();
$config = $serviceManager->get('Configuration');
$this->initSession( $config['session'] , $serviceManager->get('sessionCache') );
}
public function initSession ( $config , $cache )
{
$sessionConfig = new SessionConfig();
$sessionConfig->setOptions( $config );
$sessionManager = new SessionManager( $sessionConfig );
$saveHandler = new Cache($cache);
$sessionManager->setSaveHandler( $saveHandler );
$sessionManager->getValidatorChain()->attach('session.validate', array(new HttpUserAgent(), 'isValid'));
$sessionManager->getValidatorChain()->attach('session.validate', array(new RemoteAddr(), 'isValid'));
$sessionManager->start();
$sessionManager->regenerateId(true);
}
这些是会话的相关配置:
$config = array(
'session' => array(
'cookie_lifetime' => 28800 ,
'remember_me_seconds' => 28800 ,
'use_cookies' => true ,
'cookie_httponly' => true ,
) ,
'caches' => array(
'sessionCache' => array(
'adapter' => array(
'name' => 'memcached' ,
'lifetime' => 604800 ,
'options' => array(
'servers' => array(
array(
'127.0.0.1' , 11211
)
) ,
'namespace' => 'MySessionCache' ,
'liboptions' => array(
'COMPRESSION' => true ,
'binary_protocol' => true ,
'no_block' => true ,
'connect_timeout' => 100
)
) ,
'plugins' => array( 'serializer' )
) ,
) ,
) ,
)
知道在错误之后我的“身份”丢失并且登录的用户被注销也可能很有用。
那么,我做错了什么?如何摆脱这些错误?甚至可能:是否有更好的解决方案来设置 ACL 检查和我的会话?
更新
我更改了会话设置:
在 Module.php 中
public function onBootstrap ( MvcEvent $e )
{
$serviceManager = $e->getApplication()->getServiceManager();
/* session */
$this->expandInitialSessionConfig( $serviceManager , $serviceManager->get( 'sessionCache' ) );
}
public function expandInitialSessionConfig ( $sm , $cache )
{
/** @var \Zend\Session\SessionManager $sessionManager */
$sessionManager = $sm->get( 'Zend\Session\SessionManager' );
$saveHandler = new Cache( $cache );
$sessionManager->setSaveHandler( $saveHandler );
$sessionManager->getValidatorChain()->attach( 'session.validate' , array( new HttpUserAgent() , 'isValid' ) );
$sessionManager->getValidatorChain()->attach( 'session.validate' , array( new RemoteAddr() , 'isValid' ) );
}
在module.config.php中
return array(
'session_config' => array(
'cookie_lifetime' => 28800 ,
'remember_me_seconds' => 28800 ,
'use_cookies' => true ,
'cookie_httponly' => true ,
'gc_probability' => 10 ,
'gc_maxlifetime' => 7200 ,
'cookie_domain' => 'mydomain.com' ,
) ,
'service_manager' => array(
'factories' => array(
'Zend\Session\SessionManager' => 'Zend\Session\Service\SessionManagerFactory' ,
'Zend\Session\Config\ConfigInterface' => 'Zend\Session\Service\SessionConfigFactory' ,
) ,
);
并且 $sessionManager->regenerateId( true ) 被移动了,所以它现在只在登录时发生。
【问题讨论】:
标签: php zend-framework2 listener