Is that a good/bad/neutral practice to pass variables to Controller methods?

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

Is that a good/bad/neutral practice to pass variables to Controller methods?

dennis-fedco
Generally looking at ZF2 examples, I generally see that methods called xxxAction() contain no parameters. In fact I can't recall an example where there is a parameter passed in such manner. Should one avoid using parameter passing when calling Controller's Action methods, or is it pretty much 'whatever you feel like doing goes'? Example:
class XXX extends AbstractActionController
{
    public function indexAction()
    {
        if (...) return $this->errorAction("My Error Message 1"); 
        else if (...) return $this->errorAction("My Error Message 2"); 
        else return new ViewModel();
    }

    public function errorAction($message) //question about this parameter passing
    {
        return new ViewModel(array('content' => $message));
    }
}
Reply | Threaded
Open this post in threaded view
|

Re: Is that a good/bad/neutral practice to pass variables to Controller methods?

Julian Vidal
Dennis,

Methods that end in "Action" are supposed to be called automatically by the
framework, not by you directly. You can create your own methods with any
signature that you need. You don't have to suffix them with "Action"
though; you can name them whatever you like.


On Tue, Jun 10, 2014 at 4:20 PM, dennis-fedco <[hidden email]>
wrote:

> Generally looking at ZF2 examples, I generally see that methods called
> xxxAction() contain no parameters.  In fact I can't recall an example where
> there is a parameter passed in such manner.  Should one avoid using
> parameter passing when calling Controller's Action methods, or is it pretty
> much 'whatever you feel like doing goes'?*Example:*
> class XXX extends AbstractActionController{    public function
> indexAction()
> {        if (...) return $this->errorAction("My Error Message 1");
> else if (...) return $this->errorAction("My Error Message 2");         else
> return new ViewModel();    }    public function errorAction($message)
> *//question about this parameter passing*    {        return new
> ViewModel(array('content' => $message));    }}
>
>
>
>
> --
> View this message in context:
> http://zend-framework-community.634137.n4.nabble.com/Is-that-a-good-bad-neutral-practice-to-pass-variables-to-Controller-methods-tp4662131.html
> Sent from the Zend Framework mailing list archive at Nabble.com.
Reply | Threaded
Open this post in threaded view
|

Re: Is that a good/bad/neutral practice to pass variables to Controller methods?

Marco Pivetta
On 10 June 2014 23:21, Julian Vidal <[hidden email]> wrote:

> Methods that end in "Action" are supposed to be called automatically by the
> framework, not by you directly.


To be fair, it's not even the framework. It's handled by
`Zend\Mvc\Controller\AbstractActionController#onDispatch()`. Yes, you can't
have parameters on actions when using the `AbstractActionController`.

I like the idea of having parameters passed to controller actions, it is a
concept that I always loved since I saw JAX-RS for java (no kidding! for
those who don't know any J2EE: please go check it out!).

In fact, I and Bakura (Michael Gallego) have even worked on an entire
module that just enables this: https://github.com/zf-fr/zfr-rest

What happens in that particular module is in fact:

 1 - routing determines an "entity" or "collection" that represents the
current REST resource
 2 - the controller is resolved (like normally in a ZF2 Mvc app)
 3 - the controller action is resolved (in this context, that depends on
the HTTP verb being used)
 4 - the resource is passed to the controller action, which has a signature
like `public function patch(SomeEntity $entity) {...}`

I think this greatly simplifies your controller logic, and I'd like to
further extend these concepts to DTOs.

If you find a good way of passing parameters to action controllers without
being too opinionated, then please do share: I'd love to see more
development in this direction.


Marco Pivetta

http://twitter.com/Ocramius

http://ocramius.github.com/