DeX Posted January 24, 2017 Share Posted January 24, 2017 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. Quote Link to comment Share on other sites More sharing options...
requinix Posted January 24, 2017 Share Posted January 24, 2017 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). Quote Link to comment Share on other sites More sharing options...
DeX Posted February 7, 2017 Author Share Posted February 7, 2017 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? Quote Link to comment Share on other sites More sharing options...
requinix Posted February 7, 2017 Share Posted February 7, 2017 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.