PHP :: MVC All Controller requests through Model?
Is it best practice to have all information pass through the MODEL to the CONTROLLER?
For example, I have a controller named "Apps" and a model named "App". I want to show a form with a list of users, which is in the "Users" class in the libraries folder. Can I simply request the list of users from the "Users" class directly from the CONTROLLER or is it best to first go through the MODEL and then to the CONTROLLER?
TIA
php model-view-controller
add a comment |
Is it best practice to have all information pass through the MODEL to the CONTROLLER?
For example, I have a controller named "Apps" and a model named "App". I want to show a form with a list of users, which is in the "Users" class in the libraries folder. Can I simply request the list of users from the "Users" class directly from the CONTROLLER or is it best to first go through the MODEL and then to the CONTROLLER?
TIA
php model-view-controller
Controllers are not supposed to pull information from the Model layer. Their responsibility is to alter the state of said layer. The view classes are the ones, that are responsible for gathering the current (or changed) state from the Model layer and populating templates with it.
– tereško
Nov 21 at 11:35
add a comment |
Is it best practice to have all information pass through the MODEL to the CONTROLLER?
For example, I have a controller named "Apps" and a model named "App". I want to show a form with a list of users, which is in the "Users" class in the libraries folder. Can I simply request the list of users from the "Users" class directly from the CONTROLLER or is it best to first go through the MODEL and then to the CONTROLLER?
TIA
php model-view-controller
Is it best practice to have all information pass through the MODEL to the CONTROLLER?
For example, I have a controller named "Apps" and a model named "App". I want to show a form with a list of users, which is in the "Users" class in the libraries folder. Can I simply request the list of users from the "Users" class directly from the CONTROLLER or is it best to first go through the MODEL and then to the CONTROLLER?
TIA
php model-view-controller
php model-view-controller
asked Nov 21 at 2:00
Derwoody
324
324
Controllers are not supposed to pull information from the Model layer. Their responsibility is to alter the state of said layer. The view classes are the ones, that are responsible for gathering the current (or changed) state from the Model layer and populating templates with it.
– tereško
Nov 21 at 11:35
add a comment |
Controllers are not supposed to pull information from the Model layer. Their responsibility is to alter the state of said layer. The view classes are the ones, that are responsible for gathering the current (or changed) state from the Model layer and populating templates with it.
– tereško
Nov 21 at 11:35
Controllers are not supposed to pull information from the Model layer. Their responsibility is to alter the state of said layer. The view classes are the ones, that are responsible for gathering the current (or changed) state from the Model layer and populating templates with it.
– tereško
Nov 21 at 11:35
Controllers are not supposed to pull information from the Model layer. Their responsibility is to alter the state of said layer. The view classes are the ones, that are responsible for gathering the current (or changed) state from the Model layer and populating templates with it.
– tereško
Nov 21 at 11:35
add a comment |
3 Answers
3
active
oldest
votes
The way MVC normally works, is that the View makes a request to the Controller. The Controller then accesses the Model, handles all the business logic and updates the View.
So in your case, the User class could be your Model.
Eg.
- The View requests a list of users
- The Controller handles the request and gets the list of users from the Model (eg. through database)
- The Controller then updates the View with the list or the View has access to the list and renders it.
Here is more information.
add a comment |
As you tag your question with php
I can explain in its terms. View(HTML form or AJAX) do request to controller, usually a php file(by post or get request) what processes and redirects requests to (Model)separate php code what work with data(DB), that code returns result to controller and it one to model. Consider using some existing MVC framework what suits your needs.
Model–view–controller is an architectural pattern commonly used for
developing user interfaces that divides an application into three
interconnected parts. This is done to separate internal
representations of information from the ways information is presented
to and accepted from the user. The MVC design pattern decouples these
major components allowing for efficient code reuse and parallel
development.
https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller
add a comment |
I'm agree your point, but we must consider the size of the project. For example, in one big-size project, I usually command it like controller <=> services <=> models <=> tables.
- Tables: run sql (CURD)
- Models: call tables finish one feature (Like after get article also need update view times)
- Services: call models finish one function (Like payment)
- Controllers: Input/Permissions validator then call service finish it.
Of course, if one simple blog we can do all in controllers.
And same idea for view, may full page, may group components as one page, and component may also has childrens.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404312%2fphp-mvc-all-controller-requests-through-model%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
The way MVC normally works, is that the View makes a request to the Controller. The Controller then accesses the Model, handles all the business logic and updates the View.
So in your case, the User class could be your Model.
Eg.
- The View requests a list of users
- The Controller handles the request and gets the list of users from the Model (eg. through database)
- The Controller then updates the View with the list or the View has access to the list and renders it.
Here is more information.
add a comment |
The way MVC normally works, is that the View makes a request to the Controller. The Controller then accesses the Model, handles all the business logic and updates the View.
So in your case, the User class could be your Model.
Eg.
- The View requests a list of users
- The Controller handles the request and gets the list of users from the Model (eg. through database)
- The Controller then updates the View with the list or the View has access to the list and renders it.
Here is more information.
add a comment |
The way MVC normally works, is that the View makes a request to the Controller. The Controller then accesses the Model, handles all the business logic and updates the View.
So in your case, the User class could be your Model.
Eg.
- The View requests a list of users
- The Controller handles the request and gets the list of users from the Model (eg. through database)
- The Controller then updates the View with the list or the View has access to the list and renders it.
Here is more information.
The way MVC normally works, is that the View makes a request to the Controller. The Controller then accesses the Model, handles all the business logic and updates the View.
So in your case, the User class could be your Model.
Eg.
- The View requests a list of users
- The Controller handles the request and gets the list of users from the Model (eg. through database)
- The Controller then updates the View with the list or the View has access to the list and renders it.
Here is more information.
answered Nov 21 at 2:16
CUGreen
1,9051511
1,9051511
add a comment |
add a comment |
As you tag your question with php
I can explain in its terms. View(HTML form or AJAX) do request to controller, usually a php file(by post or get request) what processes and redirects requests to (Model)separate php code what work with data(DB), that code returns result to controller and it one to model. Consider using some existing MVC framework what suits your needs.
Model–view–controller is an architectural pattern commonly used for
developing user interfaces that divides an application into three
interconnected parts. This is done to separate internal
representations of information from the ways information is presented
to and accepted from the user. The MVC design pattern decouples these
major components allowing for efficient code reuse and parallel
development.
https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller
add a comment |
As you tag your question with php
I can explain in its terms. View(HTML form or AJAX) do request to controller, usually a php file(by post or get request) what processes and redirects requests to (Model)separate php code what work with data(DB), that code returns result to controller and it one to model. Consider using some existing MVC framework what suits your needs.
Model–view–controller is an architectural pattern commonly used for
developing user interfaces that divides an application into three
interconnected parts. This is done to separate internal
representations of information from the ways information is presented
to and accepted from the user. The MVC design pattern decouples these
major components allowing for efficient code reuse and parallel
development.
https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller
add a comment |
As you tag your question with php
I can explain in its terms. View(HTML form or AJAX) do request to controller, usually a php file(by post or get request) what processes and redirects requests to (Model)separate php code what work with data(DB), that code returns result to controller and it one to model. Consider using some existing MVC framework what suits your needs.
Model–view–controller is an architectural pattern commonly used for
developing user interfaces that divides an application into three
interconnected parts. This is done to separate internal
representations of information from the ways information is presented
to and accepted from the user. The MVC design pattern decouples these
major components allowing for efficient code reuse and parallel
development.
https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller
As you tag your question with php
I can explain in its terms. View(HTML form or AJAX) do request to controller, usually a php file(by post or get request) what processes and redirects requests to (Model)separate php code what work with data(DB), that code returns result to controller and it one to model. Consider using some existing MVC framework what suits your needs.
Model–view–controller is an architectural pattern commonly used for
developing user interfaces that divides an application into three
interconnected parts. This is done to separate internal
representations of information from the ways information is presented
to and accepted from the user. The MVC design pattern decouples these
major components allowing for efficient code reuse and parallel
development.
https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller
answered Nov 21 at 2:19
LeonidMew
1579
1579
add a comment |
add a comment |
I'm agree your point, but we must consider the size of the project. For example, in one big-size project, I usually command it like controller <=> services <=> models <=> tables.
- Tables: run sql (CURD)
- Models: call tables finish one feature (Like after get article also need update view times)
- Services: call models finish one function (Like payment)
- Controllers: Input/Permissions validator then call service finish it.
Of course, if one simple blog we can do all in controllers.
And same idea for view, may full page, may group components as one page, and component may also has childrens.
add a comment |
I'm agree your point, but we must consider the size of the project. For example, in one big-size project, I usually command it like controller <=> services <=> models <=> tables.
- Tables: run sql (CURD)
- Models: call tables finish one feature (Like after get article also need update view times)
- Services: call models finish one function (Like payment)
- Controllers: Input/Permissions validator then call service finish it.
Of course, if one simple blog we can do all in controllers.
And same idea for view, may full page, may group components as one page, and component may also has childrens.
add a comment |
I'm agree your point, but we must consider the size of the project. For example, in one big-size project, I usually command it like controller <=> services <=> models <=> tables.
- Tables: run sql (CURD)
- Models: call tables finish one feature (Like after get article also need update view times)
- Services: call models finish one function (Like payment)
- Controllers: Input/Permissions validator then call service finish it.
Of course, if one simple blog we can do all in controllers.
And same idea for view, may full page, may group components as one page, and component may also has childrens.
I'm agree your point, but we must consider the size of the project. For example, in one big-size project, I usually command it like controller <=> services <=> models <=> tables.
- Tables: run sql (CURD)
- Models: call tables finish one feature (Like after get article also need update view times)
- Services: call models finish one function (Like payment)
- Controllers: Input/Permissions validator then call service finish it.
Of course, if one simple blog we can do all in controllers.
And same idea for view, may full page, may group components as one page, and component may also has childrens.
answered Nov 21 at 3:25
incNick
35715
35715
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404312%2fphp-mvc-all-controller-requests-through-model%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Controllers are not supposed to pull information from the Model layer. Their responsibility is to alter the state of said layer. The view classes are the ones, that are responsible for gathering the current (or changed) state from the Model layer and populating templates with it.
– tereško
Nov 21 at 11:35