zf2 display module action in another module

2019-04-13 11:01发布

Hi I created two modules first application second comment. Idea is to use comment module(Widget) in any application action (website page).

Application module Test controller

public function commentAction(){
    //seting redirection for form
    $this->getCommentService()->setRedirection('test/comment');

    $list = $this->forward()->dispatch('comment_controrller', array('action' => 'list'));
    $add = $this->forward()->dispatch('comment_controrller', array('action' => 'add'));

    $view =  new ViewModel();
    $view->addChild($list, 'list');
    $view->addChild($add, 'add');
    return $view;
}

View

Comment module Comment controller

public function addAction()
{
    $form = new CommentForm();
    $form->get('submit')->setAttribute('value', 'Add');

    $request = $this->getRequest();
    if ($request->isPost()) {
        $comment = new Comment();
        $form->setInputFilter($comment ->getInputFilter());
        $form->setData($request->getPost());
        if ($form->isValid()) {
            $comment ->exchangeArray($form->getData());
            $this->getCommentTable()->saveComment($comment);

            // Redirect to test controller in application module
            return $this->redirect()->toRoute($this->getCommentService()->getRedirection());
        }
    }

    return array('form' => $form);
}

public function listAction()
{
    return new ViewModel(array(
        $list=> 'test'
    ));
}

With simple variable (list) all working fine,

Problem I get when trying to redirect form back to comment action in test controller

I can add redirection to test/comment in case form is not valid but how I will pass all validating errors to test/comment(form)

Can you tell me, if what I'm doing logically correct or in ZF2 we have different way to do widgets

1条回答
登录 后发表回答