Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 601 Vote(s) - 3.49 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Do I need to create a Model for every Controller? What is the better practise?

#1
Let's say I have a Model 'Retrieve.php' where I have a class named 'Retrieve' and it retrieves posts from a database. Then I have Controller in `Index.php` where I load that model, retrieve posts and pass it to view.

And now, I have one more page where I have to show those posts. Let's say `Sidebar.php` or something. And now again, I have to retrieve those posts. So, can I load 'Retrieve.php' once more, or I have to create one more model for `Sidebar.php` which extends 'Retrieve.php'? What is better practise?

And, in general, do I need for every controller create a new model in a good PHP MVC? If yes, probably Controller and Model should be named the same? Any more advices/comments?

Thank you.
Reply

#2
Only create the models you need. Remember, the whole point of MVC is that the models are decoupled from the views. This means it's perfectly fine to reuse whatever you need to get the job done. If you have multiple views which need access to the same data, just reuse the same model. Just be sure to give the models descriptive names so there's no confusion as to what they're supposed to represent.
Reply

#3
You should only have one Model class for each data structure that that model represents. So if you have 5 Controllers that each access the same Model, you should still only ever have one Model class.
Reply

#4
No --

Model should be what your application manages -- so instead of Retrieve, your model class should probably be Post (and maybe you have other Model classes for the nouns in your domain-- Thread, Author...)

The controllers should access the model classes they need to do their jobs; one model class could be used by several controllers, and one controller could use several model classes.
Reply

#5
In general, the model should represent a business entity and not a process. I.e., it should be a noun and not a verb. In your case, you want a model for a post, and the methods on that model will perform "the things you do with/to a post." The controller then describes what actions occur for a page. In this case, a controller for the /post page would retrieve a post and pass it to the view for rendering.
Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through