Jump to content

Should I have one main MVC view?


DeX
 Share

Recommended Posts

I'm wondering if I should initialize every view so I can reference any one I want or if I should just have a main view file that I reference and that accesses the correct view file I need.

 

The exact scenario I'm dealing with is a situation where I have a number of inputs at the top of the screen and each input has a keyup function which runs a JavaScript function to display the updated quote. It goes as so:

- JavaScript function (Ajax call)

- API file receives the Ajax call and requests the quote from the view

 

I have multiple view files and one of them is for the quote. Because it's from an Ajax call I can't just include the quote view on the page itself, it needs to be referenced from the API file and so do all of the other views needed by the API.

Link to comment
Share on other sites

Your terminology confuses me.

 

There should not be one "main" view file. Each view should be related to a controller, or be a partial view if your framework can do those, and each controller should be invoked according to the request being received by the server.

 

Process goes

1. Server receives request from client

2. Framework interprets request and locates corresponding controller

3. Controller executes and indicates which view to render

4. View renders

 

In your case the request is the AJAX stuff and eventually the view renders JSON or an HTML fragment. The controller could be this "API file", but better practice is to have one controller for each logical process (ie, API endpoint).

Link to comment
Share on other sites

  • 2 weeks later...

That makes sense, I have implemented it that way now, thanks.

Should I have separate controllers? Right now I call one controller for everything and it decides which model needs to handle the request. What if I separate them and the quote controller needs data from the user controller?

Link to comment
Share on other sites

Each logical page should have one controller. Each controller can have zero views (if it doesn't need to display anything, like if it redirects) or one view (it displays something) or multiple views (if the page is basically the same but can be displayed differently).

 

Models are tied to data. If you're using models for page data (eg, forms) then you have a model for each form, but otherwise models are not related to controllers. Most people just have models for their database entities, and controllers use the models to do whatever they need to do.

 

Models don't handle requests. Controllers handle requests.

 

Before you continue, learn what MVC actually is. Here's one of the first results I found for "php mvc" on Google: The MVC Pattern and PHP, Part 1.

Link to comment
Share on other sites

This thread is more than a year old.

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.

 Share

×
×
  • 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.