Search code examples
phpzend-frameworkzend-view

How to get view as an object into an action helper


I have a custom Action Helper that is working fine. It's generating a dynamic login box if user is not logged in, and if he is, it is generating a menu.

But here I have a problem. I want to generate that menu from a small view that's called user_menu.phtml

How I can get that view into my view helper, and assign it to an object?

Ok, some update, sorry for being stupid, actualy I have Action Helper:

I'm sorry If I was specific enough while writing my initial question.

So I have a Action helper in: library/Hlp/Action/Helper That helper renders a form, if user is not loged inn.

Here is my Helper method, that does that job:

public function preDispatch() 
{
    $view = $this->getView();

    $identity = Zend_Auth::getInstance()->getIdentity();

    $session = new Zend_Session_Namespace('users_session');
    $user_id = $session->idd;

    if( !empty($identity) ) {
        $userModel = new Application_Model_Vartotojai();
        $user_email = $userModel->geUserRowBy('id', $user_id);
        $user_email = $user_email['email'];

        $view->login_meniu = $identity.' - 
    [id:'.$user_id.']<br />['.$user_email.'] <br/>
    <a href="/authentication/logout">Log OUt</a>';
    //here I would like to read view file to an object or some other variable
    //if posible to an object si I would be able to inject some values

    } else {        
        $form = new Application_Form_LoginForm();
        $view->login_meniu = $form;
        $view->register_link = '<br /><a href="/users/register">Register</a>';
        //here I would like to read view file to an object or some other variable
    //if posible to an object si I would be able to inject some values
    } 

Additionaly to that form I want to add some links, or other HTML content, that would br stored in a view file.


Solution

  • All you have to do is to extend the Zend_View_Helper_Abstract class. Then you have the view object stored in the public property $view.

    By using that object you could render your file with

    return $this->view->partial('user_menu.phtml');
    

    Update

    Since you've updated your question I will update my answer leaving the previous answer because it's still valid for your previous question.

    In your case you already have the $view object, to do what you're asking for in the comments simply use the partial helper attached to the view in this way:

    $renderedScript = $view->partial('user_menu.phtml', 
        array('id' => $user_id, 'email' => $user_email['email']));
    

    By giving an array or an object as second argument to the partial call you can use them as model in your script file. Example:

    // content of user_menu.phtml
    <h1>Login info</h1>
    <p>
      [id: <?=$this->user_id?>]<br />
      [<?=$this->email?>] <br/>
      <a href="/authentication/logout">Log Out</a>'
    </p>
    

    P.s. I've used the short_tags + the equal sign (=) shorthand for echo in the view script, if you are not using them you should replace with <?php echo $this->email ?>