views:

217

answers:

3

Whats the best way to organize controllers. Lets say I have a user controller and a register action, should I have a process_registration action as well where I validate and process the data, or just do all processing within the register action itself. Should I have a validation/processing action for every action that requires it (register, process_registration.. etc.)

I also notice that a lot of people have modules and controllers solely for validating and processing information, ( I guess to keep all validation logic and rules in one spot maybe?)

I guess my question is, how far apart do things have to be separated? This question goes for Models and Views as Well.

+2  A: 

Generally your validation should happen in the model; that's kind of the point of the MVC paradigm. Controller logic is about bouncing the user around between actions, views are purely for presentation, and business logic sits in the models.

Certain frameworks (CodeIgniter) diverge wildly from the intent of MVC by making models flat objects that have no logic (validation or otherwise) attached, requiring you to move your validation logic into the controller layer, but at that point your "models" aren't really models at all, but glorified arrays.

As far as having two actions "register" and "process_register", I find it's much cleaner to have one action, which responds differently to post and get requests. I'd call this action "create" to keep things RESTful, and have a route defined whereever your framework defines it's routes to map "/register" to "/user/create"

Example pseudo-php:

<?php

class User_controller {
  // [GET|POST] /users/create
  function create() {
    $user = new User();
    $error = '';

    if (postback) {
      // form has been submitted.
      $user->name = $_POST['name'];
      $user->password = $_POST['pasword'];

      if (validate_user($user)) {
        $user->save();
        redirect("user/show/$user->id");
      }
      // user save failed, fall through to displaying the new user form
      // the user's name and password (and other fields) are now populated,
      // and will display
      $error = 'Account creation failed.';
    }

    // Render the view with the user and the error message (if any)
    render('views/users/create', $user, $error);
  }
}

?>
meagar
Thanks a lot, It would be great though if you could further explain how your model would look like in this example.. thanks
BDuelz
+1  A: 

My feeling is it's best to keep validation and 'processing' in the model. Use the Controller only to manage mapping the request to a model function.

This section from "Zend Framework: Surviving The Deep End" may be a good read.

Tim Lytle
A: 

I suppose the best advice I can give based on your question is that you will want to break up your actions within a controller to the degree that you want to modularize your site. You'll also have to keep in mind that there are costs in both processing and SEO if you're continuously moving from one action to another, because actions have to be associated with unique URLs.

Here's one use case where you would want to separate registration from the register action: You want to be able to register a user via AJAX in various places across the site. With a different action for going to the registration page and processing the registration data, you'll most likely be able to reuse the actions for both a registration page as well as a registration lightbox or quick-registration drawer on any page.

A case where you wouldn't want to bother splitting out registration processing and the registration page is if you're only planning on having a static registration page. That way, you can check and see if you're receiving form data and do both the form display and processing in one action.

Robert Elwell