Jump to content

Help Me With This Mvc


DeX

Recommended Posts

I'm really trying to convert my messy code into a more readable MVC architecture because it's quite a mess right now. I've been doing some research on MVC and trying to figure out how it works. I'll explain here how my web application works and I'd like someone to please tell me if I've got the right idea for setting it up for MVC.

 

Right now I have a system where a building company owner can enter customer details along with some building dimensions into a web page and it will calculate the cost of that building for that customer. It's way more complicated than this but right now I'm starting simple with the MVC conversion. When they do the quote, it gets assigned a number from the database and they can then edit this quote with new details if the customer wants another price on the building with different dimensions. So the owner goes to home.php, does the quote and the quote is generated with a database ID number. When they edit the quote, they get sent back to home.php?quote=123 but this time all the input fields are filled in with the values from the quote and the owner can now change the one thing the customer wants changed and resubmit.

 

So here's where I start asking questions. Let's just work with one of these input boxes, the customer's first name. If I understand it correctly I would have the HTML code with a call to the view to get the text for the text inside that input box. Somehow it traverses through the controller to the model where the model would check to see if the quote variable is set in the URI (home.php?quote=123). Since it is, the model would then make a call to the database to get the customer's first name associated with that quote with some SQL and a getter function would return the value. It would then traverse back through the controller to the view and be passed to the HTML code which asked for it.

 

Why do I need the controller here? Do I use it at all? Am I right in the role of the model? Would I do this for every one of the input boxes on the page? There are probably 40-50.

 

Thanks.

Link to comment
Share on other sites

It's not worth the time to implement a "pure" MVC solution. It's a lot of work for very little gain.

 

First you need to understand something. Design patterns like MVC are not prescriptions for code. They are recommendations, suggestions, and all-around advice. The lesson that MVC tries to teach is to keep the database code (model) separate from the controller (most of the application logic) separate from the view (where you render the page).

 

So,

- The controller is what takes data from the model and gives it to the view, and what takes information from the view and transfers it to the model. And all the logic that may entail.

- So yes, you need it.

- The model is the data source. It has the entities in the system. It does a variety of things with data. It doesn't necessarily have to use a database, but if that's what you need then that's what it does.

- You're using PHP so I say stick to normal PHP practices. Unless you want to turn the various forms into entities themselves (and the view simply renders it) then let the view specify the inputs and the controller grab the values from $_POST. That's perfectly fine.

- You have 40-50 inputs. There's going to be a lot of code, and a lot of that is going to be quite repetitive. You can stick things in arrays or tables or whatever other data structures you want, there's still going to be some kind of repetition somewhere.

Link to comment
Share on other sites

It's not worth the time to implement a "pure" MVC solution. It's a lot of work for very little gain.

 

First you need to understand something. Design patterns like MVC are not prescriptions for code. They are recommendations, suggestions, and all-around advice. The lesson that MVC tries to teach is to keep the database code (model) separate from the controller (most of the application logic) separate from the view (where you render the page).

 

So,

- The controller is what takes data from the model and gives it to the view, and what takes information from the view and transfers it to the model. And all the logic that may entail.

- So yes, you need it.

- The model is the data source. It has the entities in the system. It does a variety of things with data. It doesn't necessarily have to use a database, but if that's what you need then that's what it does.

- You're using PHP so I say stick to normal PHP practices. Unless you want to turn the various forms into entities themselves (and the view simply renders it) then let the view specify the inputs and the controller grab the values from $_POST. That's perfectly fine.

- You have 40-50 inputs. There's going to be a lot of code, and a lot of that is going to be quite repetitive. You can stick things in arrays or tables or whatever other data structures you want, there's still going to be some kind of repetition somewhere.

Okay so let me know if I got this right. I navigate to edit the quote and it goes to home.php?quote=555.

 

HTML

- input tag with value of $this->view->customer_first_name

 

VIEW

- customer_first_name method which returns $this->controller->get_customer_first_name()

 

CONTROLLER

- get_customer_first_name method which returns $this->model->get_customer_first_name()

 

MODEL

- get_customer_first_name method which makes a call to the database with some SQL and returns the first name of the customer associated with the quote number from $_GET['quote']

 

Is this right? Where does the quote number get accessed? Is it retrieved by the controller and passed as a parameter to the model? Or does the model get it directly? Thanks.

Link to comment
Share on other sites

I can't tell for sure but it looks like you're over-thinking the problem. It could be as simple as this:

- Request comes in to home.php?quote=555.

- Controller (home.php) handles that. The quote number comes from $_GET and the controller goes off to the model to grab that quote (as an object).

- Controller sets a variable with the quote object, or if there's no such quote displays the relevant 404 page/view.

- Normal view gets that variable and outputs whatever it wants from the quote object.

<?php // home.php

/* controller */

if (empty($_GET["quote"])) {
// no quote
}

$quote = Quote::getInstance($_GET["quote"]);
if (!$quote) {
// no such quote
}

/* end of controller */

/* view */

// blah blah blah
echo "Quote: ", $quote->name;
// blah blah blah

/* end of view */

Link to comment
Share on other sites

@Dex No, that's totally wrong and I am guessing that is due to you staring at the Desktop version of MVC instead of the Web version of it.

 

In the Web version the View does not directly update the Model, it just can't it needs the help of the Controller to do so via a GET or POST request.

 

Thus when you submit the page the Controller passes the View data ($_POST) to the Model which validates the data and through the Controller it is passed back to the View for display.

 

// Controller: contact.php

$di = StaticDIContainer::getInstance();
$req = $di->get('Request');
$model = $di->get('ContactModel');
$service = $di->get('ContactService');

if ($req->isPost()) {
 try {
   // a object can never be in an invalid state
   $model->populate($req->getPostData());
   $service->send($model);
   // success!
   $di->get('Response')->sendHeader('Location', '/');
 } catch (MultipleErrorException $e) {
   $errors = $e->getErrors();
   $di->get('View')
     ->set('errors', $errors)
     ->set('input', $req->getPostData());
 }
}

$di->get('View')->render('contact/form');

 

It's as simple as @requinix showed you though you can do it in pure procedural too.

Edited by ignace
Link to comment
Share on other sites

Nice, thank you. I have 2 questions though.

 

1. What does this part do?

$quote = Quote::getInstance($_GET["quote"]);

Where is the getInstance() method? what exactly is this doing?

 

2. How does this interact with the model to get the name from the database?

 

Thanks again.

Link to comment
Share on other sites

1. It loads a Quote from a database, which it probably also retrieves through a Singleton.

2. Quote IS the model, and it loads not only the name but the entire quote from the DB though you can easily modify what it loads by default, but will have to put things in place to load extra stuff when needed.

Edited by ignace
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.