zf2 widget base app logic

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

zf2 widget base app logic

NikolaiSenkevich
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

<?php echo $list; ?>
<?php echo $add; ?>


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

Thanks

Nikolai
Reply | Threaded
Open this post in threaded view
|

Re: zf2 widget base app logic

weierophinney
Administrator
-- NikolaiSenkevich <[hidden email]> wrote
(on Wednesday, 10 October 2012, 02:39 AM -0700):

> 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'));

Don't do it this way. Using forward() is brittle.

I'd do the following:

 * implement the comment form and comment list rendering as view
   helpers, and create factories to attach to the view_helpers
   configuration for injecting each with their dependencies.
   - rendering a comment form would accept a comment form instance
     - you would pass the helper the URL to the current page, and
       potentially the ID of the parent comment (if doing threading).
   - rendering a comment list would accept the comment model
     - calling something like comments() or commentList() would accept the
       URL of the current page, and use that to retrieve comments
 * For processing the comments, have a single controller action, *in*
   the comment module. Typically, the only view it would need to display
   is the "error validating" view -- and this does not need to be on the
   same page as the original. Ideally, the action would allow requests
   via XHR, so that the submission can happen in-page.

Using this approach will keep the logic for a consuming module simpler
-- controllers require no changes at all, and at most you simply invoke
the appropriate helpers in your view scripts.

> $view =  new ViewModel();
>         $view->addChild($list, 'list');
>         $view->addChild($add, 'add');
>         return $view;
>     }
>
> View
>
> <?php echo $list; ?>
> <?php echo $add; ?>
>
>
> 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
>
> Thanks
>
> Nikolai
>
>
>
> --
> View this message in context: http://zend-framework-community.634137.n4.nabble.com/zf2-widget-base-app-logic-tp4657457.html
> Sent from the Zend MVC mailing list archive at Nabble.com.
>
> --
> List: [hidden email]
> Info: http://framework.zend.com/archives
> Unsubscribe: [hidden email]
>
>

--
Matthew Weier O'Phinney
Project Lead            | [hidden email]
Zend Framework          | http://framework.zend.com/
PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc

--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]


Reply | Threaded
Open this post in threaded view
|

Re: zf2 widget base app logic

NikolaiSenkevich
Hi
Thanks for you answer
Can you just have look:
https://github.com/nsenkevich/comment/tree/master/Comment
If this make sence for you ?
Or I missed something




--
View this message in context: http://zend-framework-community.634137.n4.nabble.com/zf2-widget-base-app-logic-tp4657457p4657653.html
Sent from the Zend MVC mailing list archive at Nabble.com.

--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]


Reply | Threaded
Open this post in threaded view
|

Re: zf2 widget base app logic

NikolaiSenkevich
This post was updated on .
In reply to this post by weierophinney
Hi
Thanks for you answer
Can you just have look: https://github.com/nsenkevich/comment/tree/master/Comment
If this make sence for you ?
Or I missed something

and  one more question

If i will need diferent list coments: per project, per user, per admin (all comments in system)

Should I create 3 view helpers or I can use Strategy and defence on parameter passing in view helper generate correct list comments

or should I use something else

Thanks