tags:

views:

503

answers:

1

Hey out there,

I'm teaching myself Zend am and having a problem with using my session to call a View Helper action.

My controller:

<?php
class SessionController extends Zend_Controller_Action
{
    protected $session;
    public function init() //Like a constructor
    {
      $this->_helper->viewRenderer->setNoRender(); // Will not automatically go to views/Session
     $this->_helper->getHelper('layout')->disableLayout(); // Will not load the layout
    }  

    public function preDispatch() //Invokes code before rendering.  Good for sessions/cookies etc.
    {
     $this->session = new Zend_Session_Namespace(); //Create session
        if(!$this->session->__isset('view'))
        {
            $this->session->view = $this->view; //if the session doesn't exist, make it's view default
        }

    }
    public function printthingAction()
    {
        echo $this->session->view->tabbedbox($this->getRequest()->getParam('textme'));
    }
}
?>

My view helper

<?php
class App_View_Helper_Tabbedbox extends Zend_View_Helper_Abstract
{
    public $wordsauce = "";
    public function tabbedbox($message = "")
    {
        $this->wordsauce .= $message;
        return '<p>' . $this->wordsauce . "</p>";
    }
}
?>

My view:

<p>I GOT TO THE INDEX VIEW</p>

<input id='textme' type='input'/>
<input id='theButton' type='submit'/>

<div id="putstuffin"></div>

<script type="text/javascript">
$(function()
{
    $("#theButton").click(function()
    {
        $.post(
        "session/printthing",
        {'textme' : $("#textme").val()},
        function(response)
        {
            $("#putstuffin").append(response);
        });
    });
});

</script>

The first time I click on theButton, it works, and appends my word like it's supposed to. For every time after, though, it gives me this error message:

Warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, '__PHP_Incomplete_Class::tabbedbox' was given in C:\xampp\htdocs\BC\library\Zend\View\Abstract.php on line 341

I copied the Zendcasts.com video almost line for line, and it's still not working. It seems like my session is getting destroyed or something. I would be forever grateful to anyone who could tell me what's happening.

+1  A: 

When you store an object in the session, you're really storing a serialized representation of it. The __PHP_Incomplete_Class::tabbedbox occurs because, on subsequent requests, PHP has forgotten what an App_View_Helper_Tabbedbox is.

The solution: make sure you include the App_View_Helper_Tabbedbox class file before Zend_Session::start() is called.

And, the best way to do that is to place this at the opening of your app:

require_once 'Zend/Loader.php';
Zend_Loader::registerAutoload();
that's it! thank you.
Ethan