Note that there are some explanatory texts on larger screens.

plurals
  1. POWhere to validate and process form submission in MVC website
    primarykey
    data
    text
    <p>I'm working on a PHP based Model-View-Controller structured website. I understand that the Models should deal with business logic, views present HTML (or whatever) to the user, and the controllers facilitate this. Where I'm running stuck is with forms. <strong>How much processing do I put in the controller, and how much do I put the my model?</strong></p> <p>Assume that I'm trying to update a user's first &amp; last name. What I want to do is submit a form using AJAX to one of my controllers. I want the data to be validated (again) server side, and if valid save it to the database, and then return a JSON response back to the view, as either a success or error.</p> <p>Should I create an instance of my user model in the controller, or should I just have the controller relay to a static method in my model? Here is two examples of how this could work:</p> <p><strong>Option #1: Process POST in Model</strong></p> <pre><code>&lt;form action="/user/edit-user-form-submit/" method="post"&gt; &lt;input type="text" name="firstname"&gt; &lt;input type="text" name="lastname"&gt; &lt;button type="submit"&gt;Save&lt;/button&gt; &lt;/form&gt; &lt;?php class user { public function __construct($id){} // load user from database public function set_firstname(){} // validate and set first name public function set_lastname(){} // validate and set last name public function save_to_database(){} // save object fields to database public static function save_data_from_post() { // Load the user $user = new user($_POST['id']); // Was the record found in the db? if($user-&gt;exists) { // Try to set these fields if( $user-&gt;set_firstname($_POST['firstname']) and $user-&gt;set_lastname($_POST['lastname']) ) { // No errors, save to the dabase $user-&gt;save_to_database(); // Return success to view echo json_encode(array('success' =&gt; true)); } else { // Error, data not valid! echo json_encode(array('success' =&gt; false)); } } else { // Error, user not found! echo json_encode(array('success' =&gt; false)); } } } class user_controller extends controller { public function edit_user_form() { $view = new view('edit_user_form.php'); } public function edit_user_form_submit() { user::save_data_from_post(); } } ?&gt; </code></pre> <p><strong>Option #1: Process POST in Model</strong></p> <pre><code>&lt;form action="/user/edit-user-form-submit/" method="post"&gt; &lt;input type="text" name="firstname"&gt; &lt;input type="text" name="lastname"&gt; &lt;button type="submit"&gt;Save&lt;/button&gt; &lt;/form&gt; &lt;?php class user { public function __construct($id){} // load user from database public function set_firstname(){} // validate and set first name public function set_lastname(){} // validate and set last name public function save_to_database(){} // save object fields to database } class user_controller extends controller { public function edit_user_form() { $view = new view('edit_user_form.php'); } public function edit_user_form_submit() { // Load the user $user = new user($_POST['id']); // Was the record found in the db? if($user-&gt;exists) { // Try to set these fields if( $user-&gt;set_firstname($_POST['firstname']) and $user-&gt;set_lastname($_POST['lastname']) ) { // No errors, save to the dabase $user-&gt;save_to_database(); // Return success to view echo json_encode(array('success' =&gt; true)); } else { // Error, data not valid! echo json_encode(array('success' =&gt; false)); } } else { // Error, user not found! echo json_encode(array('success' =&gt; false)); } } } ?&gt; </code></pre> <p>The two examples do the exact same thing, I realize that. But is there a right and wrong way of doing this? I've read a lot about skinny controllers and fat models, where is where option #1 came from. How are you handling this? Thanks, and sorry for the long question!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload