Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7705073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:48:24+00:00 2026-05-31T23:48:24+00:00

I’m looking for best way of using session within zf application. At first I

  • 0

I’m looking for best way of using session within zf application.
At first I did something like this – in init method of controller superclass I initialized session:

class Vovkin_Controller_Action extends Zend_Controller_Action
{
    protected $_session;

    public function init()
    {   
        // here I define namespace 
        // ...
        $this->_session = new Zend_Session_Namespace($nameSpace);
        parent::init();
    }
    ...
}

after that session in controller was used in this way:

public function someAction()
{
    $this->_session->user = $user;
}

but I found this approach not very handy for other parts of system, like plugins, services, etc, because there I had to init session in other way, but I want keep it in one place if it’s possible. So I decided to change it to this approach https://stackoverflow.com/a/2506447.

Now I have a few action helpers to provide access for sessions with different namespaces, it works like this:

public function someAction()
{
    $this->_helper->session()->user = $user;
}

and so far it looks useful, because I can get access to session namespaces in other parts of system, for example in services, in this way:

class Vovkin_Model_Service_UserLoginService
{
    public function login()
    {
        $session = Zend_Controller_Action_HelperBroker::getStaticHelper('session')->direct();
        ...
    }   
    ....
}

but how much it’s correct to use it in this way, from point of architecture and used resources?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T23:48:24+00:00Added an answer on May 31, 2026 at 11:48 pm

    The answer you have refereed to is from Rob Allen, one of the main contributors of Zend Framework, so it’s right to some extent. You can go with the action helpers on controllers without any problems.

    But outside it, it’s completely wrong. Services doesn’t has nothing with action helpers and the front controller. You can’t put a dependency on it (services to action helpers).

    So, as the application bootstrap works as a container for initializing the application, it’s reasonable to get the necessary values from there. The first thing I would suggest you is to use Zend_Registry. But as far as I cal tell you, it would be the same initializing the session object again, since it will not be wiped, it’s just an object referencing the native $_SESSION superglobals. So, simply call

    new Zend_Session_Namespace($nameSpace).

    But again, this is wrong. You should not let your services know how sessions are handled (thus creating the objects inside it):

    $session = Zend_Controller_Action_HelperBroker::getStaticHelper('session')->direct()
    

    or even

    $session = Zend_Registry('userSession')

    or

    $session = new Zend_Session_Namespace('userSession')

    With that you are also not using the bootstrap container at all. Instead you should provide a common interface to deal with sessions (it could be $_SESSION or even a database) and inject it into the service as a parameter (e.g. __construct($session)). But that’s a whole new subject (Dependency Injection).

    So, you have two options considering the current state of the ZendFramework 1.11 (that’s already old and full of bad practices):

    1) You’ll use services through controllers:

    So you will get the session through the action helper and then pass it as a parameter to your service.

    new Vovkin_Model_Service_UserLoginService($session)
    

    2) You will use services independently of controllers and will get the dependencies through the bootstrap container:

    Well, the worst thing is that to get the bootstrap you need to have a frontController dependency.

    $bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');
    

    So, avoid it, and go with the first option, injecting the dependency instead. Although, if you really want it in that way, access it directly:

    $bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');
    $bootstrap->get('userSession')
    

    Sadly, in the end, it’s everything wrong. But it’s the best you can do with ZF 1. You should look forward to ZF 2 and Symfony 2 to better understand these concepts.

    Here it’s a good explanation: http://symfony.com/doc/current/book/service_container.html
    (I know that you’re using ZF, but it doesn’t matter, the concept is the key)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have some data like this: 1 2 3 4 5 9 2 6
I'm making a simple page using Google Maps API 3. My first. One marker
I have a jquery bug and I've been looking for hours now, I can't
I would like to count the length of a string with PHP. The string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.