1

I hope the ZF2 gurus can help me out here a little bit. I just started converting a project of mine from ZF1 to ZF2 and I've stumbled upon a segment of my code that I don't really know how to re-write them in ZF2. Here's the problem.

I have a page where users view a selected blog post. In the same page, I have a view helper that displays all the comments for the post like this:

<?php echo $this->comments($blog->id); ?>

When the user submits a comment in the form, the comment is transmitted through the getJSON function (jQuery) to a controller. The controller would store the comment and return true/false to the getJSON function, depending on whether there's any problem with their comment.

What happens next is the same controller and the same action is suppose to echo the view helper and the user will see that the newly submitted comment gets displayed within that same page. In my ZF1 code, I was able to do this by having the following codes in the controller:

First up, a preDispatch function is defined in the ZF1 controller:

public function preDispatch() {
        $this->session = new Zend_Session_Namespace('default');
        if (!$this->session->view) {
            $this->session->view = $this->view;
        }
    }

Then, somewhere in the savecommentAction(), I just have to do this:

echo $this->session->view->Comments();

And the comments in the user's view would be updated.

Does anyone know how can I do the above in ZF2? I've been trying to figure a way to do this in ZF2 with the ViewModel but I've been scratching my head for the past 48 hours that I would soon run out of hair to scratch. Hope anyone of you kind souls could help me figure out what am I missing to do this.

Roy
  • 13
  • 2
  • 1
    I'd simply do the JS-Comment-Add and if i get a true response i would manually manipulate the DOM to render the new comment (the data is there anyways) - no need to have the webserver render something – Sam Mar 15 '14 at 15:22
  • I've actually considered that as the last resort because each user's comment is wrapped in many HTML tags. That's one of the primary reason why I have a view helper created in the first place and the view helper does much more than just displaying the lines of comments as it would include the retrieval of the commentator's avatar, name, etc. – Roy Mar 15 '14 at 16:07
  • Well a ViewHelper would be a wrong approach. You'd simply do your request and you have two possible responses 1) false 2) rendered html :) the rendered html then is simply a single rendered commend – Sam Mar 15 '14 at 16:20

1 Answers1

0

if you just want to run the comment view helper from your action you can get it from view helper manager :

$this->getServiceManager()->get('ViewHelperManager')->comments()

UPDATE: sorry its serviceLocator not manager, use this code

$this->getServiceLocator()->get('ViewHelperManager')->comments()
Exlord
  • 5,009
  • 4
  • 31
  • 51
  • I tried putting this in the controller action but the getServiceManager() doesn't seem to show up in my IDE. Does the above work if I have the ViewModel set to Terminal mode? – Roy Mar 16 '14 at 06:24
  • updated the answer , "Does the above work if I have the ViewModel set to Terminal mode" it doesn't matter – Exlord Mar 16 '14 at 08:43
  • Thanks Exlord and Sam. I combined the suggestion from both of you and was able to got it working. – Roy Mar 16 '14 at 15:41