Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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.</p> <p>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).</p> <p>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 <code>Zend_Registry</code>. 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 <code>$_SESSION</code> superglobals. So, simply call </p> <p><code>new Zend_Session_Namespace($nameSpace)</code>.</p> <p>But again, this is wrong. You should not let your services know how sessions are handled (thus creating the objects inside it): </p> <pre><code>$session = Zend_Controller_Action_HelperBroker::getStaticHelper('session')-&gt;direct() </code></pre> <p>or even </p> <p><code>$session = Zend_Registry('userSession')</code></p> <p>or </p> <p><code>$session = new Zend_Session_Namespace('userSession')</code></p> <p>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. <code>__construct($session)</code>). But that's a whole new subject (Dependency Injection). </p> <p>So, you have two options considering the current state of the ZendFramework 1.11 (that's already old and full of bad practices):</p> <p><strong>1) You'll use services through controllers:</strong></p> <p>So you will get the session through the action helper and then pass it as a parameter to your service.</p> <pre><code>new Vovkin_Model_Service_UserLoginService($session) </code></pre> <p><strong>2) You will use services independently of controllers and will get the dependencies through the bootstrap container:</strong></p> <p>Well, the worst thing is that to get the bootstrap you need to have a frontController dependency.</p> <pre><code>$bootstrap = Zend_Controller_Front::getInstance()-&gt;getParam('bootstrap'); </code></pre> <p>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:</p> <pre><code>$bootstrap = Zend_Controller_Front::getInstance()-&gt;getParam('bootstrap'); $bootstrap-&gt;get('userSession') </code></pre> <p>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.</p> <p>Here it's a good explanation: <a href="http://symfony.com/doc/current/book/service_container.html" rel="nofollow">http://symfony.com/doc/current/book/service_container.html</a> (I know that you're using ZF, but it doesn't matter, the concept is the key)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload