php - MVC - A place for common functions?
Get the solution ↓↓↓Solution:
I tend to think of traditionalMVC
like this:
A controller is my business logic to implement my product.
A model is the code to represent data.
A view is what represents my product.
Under this paradigm,what represents my product
is what an end-user sees.
So if it's a UX component, it's part of the view.
In this way, it's pretty easy to figure out how to use it:
- You could
use path/to/notifications.php
and then$notifications = new Notifications();
wherenotifications.php
is a class facilitating messages like you reference. - You could
extends Notifications
on your view (or even a base view!) so that ANY view you instantiate immediately has access to your notifications class. - You could (though probably shouldn't)
require_once( 'path/to/notifications.php' );
and then the class or functions therein would be available. Again, you probably shouldn't do it this way
The bottom line is this:
- Place your old procedural code in a well organized class. It might be named something like
Notifications
if that's what the code does. The important thing is to organize well, name well, and do not try to force too much code into one thing. - Use that class in your code. You can explicitly use it's namespace and
new
an instance of it, or you can extend it to bring it along as the parent class of your view.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: the payload is invalid.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.