1

When designing laravel applications within the MVC pattern is it ok to have controllers that are not related to a model?

For example: My app has "Event", "User" and "Activity" models and related controllers. However, the application dashboard will be displaying a news feed of events, users and other items. Should I create a DashboardController class? Even if this does not directly relate to a model class?

Or is there a better way of doing this?

1 Answer 1

3

Sure you can just make calls to the various models you need from the dashboard controller. I'm doing something similar myself at the moment and I have a dashboard controller in the works.

MVC doesn't mean that every C has to have an M or even a V. ;) Think of it as an overall pattern/structure and not that each has to be linked to the next level.

Sign up to request clarification or add additional context in comments.

4 Comments

although is this best practice? having controllers that have no model relating to them directly?
Sure, you don't want to end up having a *Dashboard method in each controller. You can simply call each of those models in your Dash controller and build up the data you need to show.
Think of MVC as a suggested structure for separation of your code. Not an enforced ruleset that means every part of your app has to have all 3 parts.
(Example) you don't have a dashboardcontroller, and over the years your application has grown to over 20 different dashboards. When a vital change has to be made in your dashboard, lets say AuthorizationAttributes. You would have to edit this in every controller you have that holds a dashboardmethod. And again if the AuthorizationAttribute changes. In the case of a seperate controller for dashboards your changes would be on a single file, or even a single method. This is a blessing when working with customer's shifting requirements.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.