Search code examples
sessionzend-framework2

Zend Framework Session Management. Zend_Session_Namespace not found


I am referring to this documentation on how to use sessions with Zend Framework: http://framework.zend.com/manual/1.12/en/learning.multiuser.sessions.html

I have started with a skeletal application at this point the only thing I have modified is the controller. I am posting the code for Application module controller:

            <?php
            /**
             * Zend Framework (http://framework.zend.com/)
             *
             * @link      http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
             * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
             * @license   http://framework.zend.com/license/new-bsd New BSD License
             */

            namespace Application\Controller;

            use Zend\Mvc\Controller\AbstractActionController;
            use Zend\View\Model\ViewModel;

            class IndexController extends AbstractActionController
            {
                public function indexAction()
                {
                    $mysession = new Zend_Session_Namespace('mysession');

                    if (!isset($mysession->counter)) {
                        $mysession->counter = 1000;
                        } else {
                        $mysession->counter++;
                    }

                    if ($mysession->counter > 1999) {
                        unset($mysession->counter);
                    }

                    return new ViewModel();
                }
            }

The first thing I see when I go to the route is:

            Fatal error: Class 'Application\Controller\Zend_Session_Namespace' not found in C:\Program Files (x86)\Zend\Apache2\htdocs\zf2-sessioncounter\module\Application\src\Application\Controller\IndexController.php on line 19

So I am thinking this has to do with a use Zend_Session_Namespace at this point or perhaps it is because my application is not set up for Zend_Application? Would this be a correct place to put the session php?


Solution

  • Zend_Session_Namespace is actually a component of ZF1 - which shouldn't be used in ZF2 application because of many reasons (NOT using proper namespace mechanism being the most important).

    Try Zend\Session\Container instead:

    use Zend\Session\Container;
    // ...
    
    $mysession = new Container('mysession');