duanlei0282 2016-01-06 16:50
浏览 18

用于复杂页面的Laravel控制器

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          -> userController@list     -> userList.blade.php

/listallclients        -> clientContoller@list    -> clientList.blade.php
/listdetailofoneclient -> clientContoller@details -> 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?
  • 写回答

1条回答 默认 最新

  • douhuxi4145 2016-01-06 17:29
    关注

    I'll take a shot at a couple of these questions.

    1. Having a PageController or HomeController is very acceptable. Controllers don't have to be linked to a specific model, for instance an AuthController would handle logic for logging in and out but isn't tied to a model, or PasswordController which handles setting/resetting passwords, or a PaymentController that handles billing routes. A controller is just a way to organize logic for related routes in one file. Static basic pages are related routes so a PageController makes sense to me.

    2. Are users tied to a specific client? If so you can arrange your controllers in a nested way so you have a ClientController and a UserController and your routes look something like this:

      /clients/{client}/users   //list all users for this client
      
    3. For heavy data formatting it's probably best to use a service provider and use dependency injection to inject it into controllers where you need to use it. This allows you to detach your data manipulation from your controller so you could change it out if needed. Say you are using a software to make charts, and you want to change it out later - you want that formatting logic to be removed from your controller.

    I hope I helped in some way... sort of a train of thought here!

    评论

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)