Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

zend framework 3 session not working

I'm trying to set up a zend framework 3 MVC web app to use session storage. Following the information from this website --

https://olegkrivtsov.github.io/using-zend-framework-3-book/html/en/Working_with_Sessions/PHP_Sessions.html

It all works well. I get the session variable in my controller and I can save data to the session container just fine. The problem is, the data I save to the container is NOT there on subsequent calls. I'm saving search criteria from one page and doing a redirect to a second page to do the search and return the results. The session data is not present when I enter the second page.

In config\global.php I have --

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,
    ],
];

In application\module.php i have modified 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);
 }

I created an 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);
    }
}

Modified my IndexController to add a constructor method

class IndexController extends AbstractActionController
{
    private $session;

    public function __construct(Container $session)
    {
        $this->session = $session;
    }

In application\module.config.php i have this

'controllers' => [
    'factories' => [
        Controller\IndexController::class => Controller\Factory\IndexControllerFactory::class,
    ],
],
'session_containers' => [
    'Books\Session'
],
like image 606
ScottTx Avatar asked Oct 18 '22 21:10

ScottTx


1 Answers

To store something in session you can create the container as follows:

// Create a session container
$container = new Container('Books\Session');
$container->key = $value;

To retrieve something from a session container later you have to create a new container with the same name:

// Retrieve from session container
$container = new Container('Books\Session');
$value = $container->key;

As far as I know this works similarly for both ZF2 and ZF3 and can be found in other posts on StackOverflow or for example this blog post with the title Using Sessions in Zend Framework 2 .

If you create a new Container for storing or resolving data from the session it will automatically use the default session manager if you don't pass one yourself.

You can see that here in the AbstractContainer::__construct method on line 77. If the $manager passed to the constructor is null it will get the default session manager inside the setManager method.

So to use sessions you don't need to do a lot of manual configuration.

If that doesn't solve your problem please leave a comment.

like image 196
Wilt Avatar answered Oct 28 '22 23:10

Wilt