[英]zend framework 3 session not working
我正在嘗試設置一個zend framework 3 MVC Web應用程序以使用會話存儲。 遵循本網站的信息-
一切都很好。 我在控制器中獲取了會話變量,並且可以將數據保存到會話容器中。 問題是,我保存到容器的數據在后續調用中不存在。 我要從一頁保存搜索條件,然后重定向到另一頁以執行搜索並返回結果。 當我進入第二頁時,會話數據不存在。
在config \\ global.php中,我有-
return [
'session_config' => [
// Cookie expires in 1 hour
'cookie_lifetime' => 60*60*1,
// Stored on server for 30 days
'gc_maxlifetime' => 60*60*24*30,
],
'session_manager' => [
'validators' => [
RemoteAddr::class,
HttpUserAgent::class,
],
],
'session_storage' => [
'type' => SessionArrayStorage::class,
],
];
在application \\ module.php中,我修改了onBoostrap
public function onBootstrap(MvcEvent $event)
{
$application = $event->getApplication();
$svcMgr = $application->getServiceManager();
// Instantiate the session manager and
// make it the default one
//
$sessionManager = $svcMgr->get(SessionManager::class);
}
我創建了一個IndexControllerFactory
class IndexControllerFactory implements FactoryInterface
{
public function __invoke(ContainerInterface $container,
$requestedName, array $options = null)
{
// Get access to session data
//
$sessionContainer = $container->get('Books\Session');
return new IndexController($sessionContainer);
}
}
修改了我的IndexController以添加構造函數方法
class IndexController extends AbstractActionController
{
private $session;
public function __construct(Container $session)
{
$this->session = $session;
}
在application \\ module.config.php我有這個
'controllers' => [
'factories' => [
Controller\IndexController::class => Controller\Factory\IndexControllerFactory::class,
],
],
'session_containers' => [
'Books\Session'
],
要在會話中存儲內容,可以按如下方式創建容器:
// Create a session container
$container = new Container('Books\Session');
$container->key = $value;
要稍后從會話容器中檢索內容,您必須創建一個具有相同名稱的新容器:
// Retrieve from session container
$container = new Container('Books\Session');
$value = $container->key;
據我所知,這對於ZF2和ZF3都類似,並且可以在StackOverflow上的其他帖子中找到,例如在Blog標題為“在Zend Framework 2中使用會話”中 。
如果您創建一個新的Container
來存儲或解析會話中的數據,那么如果您自己沒有通過Container
,它將自動使用默認的會話管理器。
您可以在第77行的AbstractContainer::__construct
方法中看到這一點 。 如果傳遞給構造函數的$manager
為null
,它將在setManager
方法中獲取默認的會話管理器。
因此,使用會話無需進行大量手動配置。
如果那不能解決您的問題,請發表評論。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.