-- Ralf Eggert <[email protected]> wrote
(on Tuesday, 18 September 2012, 04:29 PM +0200):
> Hi Matthew,
> 
> > Then you need to break your controller into multiple controllers, as
> > it's managing too much.
> > 
> > The rule of thumb I have is: more than 5-7 actions in a controller:
> > refactor. More than 1-2 forms: refactor. Otherwise, the workflow of the
> > controller becomes too difficult to follow easily.
> 
> Interesting stuff. I am thinking about this, but the rules of thumb
> might not fit on every case I think.
> 
> Here is an example controller I use for managing users by an admin:
> 
> ---------------------------------------------------------------------
> class AdminController
> {
>     public function indexAction()
>     {
>         // uses select form
>     }
> 
>     public function addAction()
>     {
>         // uses create form
>     }
> 
>     public function updateAction()
>     {
>         // uses update form
>     }
> 
>     public function deleteAction()
>     {
>         // uses delete form
>     }
> }
> ---------------------------------------------------------------------
> 
> These are 4 actions and 4 forms. Would you really split this up in more
> than one controller?

Well, one question I have is: is it really 4 forms? or 1 form with 4
views? There's a difference. :)

By using setValidationGroup(), you can have a single form representing a
user, but validate different pieces of it based on the action. For the
"add" action, you'd have one set of fields, for update, another, and for
delete, likely simply the user identifier. Your view scripts would
determine how much or little of the form needs to be displayed. As such,
the above would fit into the criteria I outlined quite well

> Here is another controller I have
> 
> ---------------------------------------------------------------------
> class UserController
> {
>     public function indexAction()
>     {
>     }
> 
>     public function registerAction()
>     {
>         // uses create form
>     }
> 
>     public function updateAction()
>     {
>         // uses update form
>     }
> 
>     public function showAction()
>     {
>     }
> 
>     public function activateAction()
>     {
>     }
> 
>     public function loginAction()
>     {
>         // uses login form
>     }
> 
>     public function logoutAction()
>     {
>     }
> 
>     public function passwordAction()
>     {
>         // uses password form
>     }
> 
>     public function forbiddenAction()
>     {
>     }
> }
> ---------------------------------------------------------------------
> 
> This controller has 9 actions which use 4 forms. Well, I could move
> loginAction(), logoutAction() and forbiddenAction() to an AuthController
> and maybe registerAction() and activateAction() to a
> RegistrationController.

Exactly -- and I'd do this, as this more semantically groups the
actions.

> But does that really make sense?

Yes -- at least in my opinion. :)

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

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


Reply via email to