Laravel controllers for complex pages

I have started a new laravel website project and I have hit a road block in my understanding of MVC. I need to refactor to continue but I don’t know the best way.

Currently I have web pages that display the results of a single bit of logic. I.e. A page listing all users, a page listing the details of just one user etc etc – all handled by a userController. This applies to other pages being handled by other controllers.

I have created models directly relating to the tables in my database, and controllers in relation on the models. I moved the business logic from the controllers to services. The controllers use the services to perform the business logic and with the data returned, pass that data to the views.

This nicely groups similar functionality together and works fine.

userTable -> userModel -> userController -> userService
clientTable -> clientModel -> clientController -> clientService

In my routes, I have pages which do related functionality use the same respective controller, but individual methods depending on what the page does

/listallusers -> [email protected] -> userList.blade.php

/listallclients -> [email protected] -> clientList.blade.php
/listdetailofoneclient -> [email protected] -> clientDetails.blade.php

This is ok when dealing with pages that do that functionality and (apart from using services) seems to be what is hinted at in the laravel docs.

However, I’m starting to get confused about controllers when dealing with pages that either don’t really use functionality from any of the services or pages that heavily require functionality from multiple services (and the data needs complex manipulation like formatting or such).

  1. A basic index page.

What controller would handle this? The index might link to routes that are handled by existing controllers but it probably won’t need to display much functionality apart from that. That means the controller won’t need to pass much complex -if any- data to the view. You could stick the logic to return the view in the route file but that is pretty tightly coupled.

  1. A page that shows complex client and user data

You need to pass client data and user data to the view from the controller. But from what controller? This is the part that is really holding me back.

Because I have a limited number of pages but lots of logic displaying on each page, I was thinking of making a page controller (or something) which would handle the routing. Although I have looked, I have not seen any real mention of this idea anywhere which makes me think I am either reinventing the wheel or have failed to grasp some basic concept in laravel / MVC.

Would the page controller in this case handle all the routing? Would it handle only the pages with ‘overlapping’ existing controller functionality and the pages that don’t fall into the existing controllers? Is a page controller even a good idea?

Some more points that have made me question MVC from this issue

  • Do controllers need an equivalent model?
  • Can controllers ‘control’ other controllers in order to separate logic?

from Newest questions tagged laravel-5 – Stack Overflow

Related Posts

Codeigniter : Parse error: syntax error, unexpected ‘const’ (T_CONST), expecting variable (T_VARIABLE) in Laravel project

I’m getting following error: **Parse error: syntax error, unexpected ‘const’ (T_CONST), expecting variable (T_VARIABLE)** Note : It’s working in local but facing issue in production server. private…

Firebase receive notification while tab is active or on focus

What i want is to be able to perform an action when a user receives a notification while the browser is open and tab is active or…

Laravel’s alias loader does not find class

We have a legacy project that we cannot update and we need to make some changes in symfony’s Response.php in vendor. We have solved this by copying…

Laravel 5 – generic document management

I have a system where you can create different types of unique documents. For instance, one document is called Project Identified and this expects certain inputs. Originally,…

Laravel Nova limit the results in indexQuery

I ran intro a situation where I need to limit the results of a resource to only 3 results. To be more specific, based on the logged…

Auditoria en laravel 5.8 [closed]

Cómo puedo automatizar el registro de actividades de un usuario en laravel? Si un usuario ingresa a un app de laravel, debo guardar toda su actividas, a…

Leave a Reply

Your email address will not be published.