문제

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.

도움이 되었습니까?

해결책

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()
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top