dsuikgi9199 2019-01-16 12:25
浏览 20
已采纳

更好的方式#PHP MVC [关闭]

PHPMVC

Is it a good practice to handle all calls with one controller only ?

class Controller {

invoke()

SignIn()

create()

admin()

}

or iscenario?if different controller for every scenario ?

  • 写回答

1条回答 默认 最新

  • duanjia4817 2019-01-16 12:45
    关注

    A Controller is the glue between your business logic and the requests made. Although it can contain quite a few functionalities, they should all be specific to the target request in question.

    A a small description of a controller, you will find similar cases as:

    Controllers are the glue that binds models, views and other components together into a runnable application. Controllers are responsible for dealing directly with end user requests. Therefore, controllers

    Understanding that, to keep your controllers focused you need to ask yourself the question: Is this functionality referred by the controller?

    You can find inspiration in the laravel way of organizing their MVC. You will notice that they separated all authentication requests into a AuthController.

    This AuthController is responsible for:

    -> capturing the POST request that will contain the password and the email (or whatever other method you created).

    -> Authenticate the user if successful; -> redirect to the correct views depending on the results for auth (back to login page or to show the dashboard);

    The latest is where you have to start organizing your flow. Check this out:

    -> If the user was succesfully authenticated, then you wish to present the dashboard view;

    -> the dashboard view is not really part of the AuthController is more directly related to a DashboardController. So, you will actually want to redirect from the AuthController to the DashboardController via routes;

    So the answer to your question is, it depends! If the logic you are adding to your controller is focusing on a specific business logic sector of your application, do not be bothered by the amount of methods you might have. It all really depends of the complexity of your application;

    BUT if you controller starts having methods that do many different things for many different sectors of you application, lets say you have a controller that:

    ->creates products
    ->deletes products;
    ->Authenticates users;
    ->list users;
    -> etc
    

    Then you are doing it wrong and not separating the business logic accordingly.

    The controller responsibility is to glue together the request with the correct business logic and then redirect all the data to a correct view to display it. It shouldn't be aware on:

    -> How the data is fetched (doing the Model job);
    -> How the data should be parsed for display (doing the Marshaller job);
    -> Checking if the data exists (doing the Hydrator job);
     among other concerns. It literally does:
    
    
    1. Oh! got a request from route `list/users`;
    2. To list users I better call all users in my database (Call the Model);
    3. Right, I believe they should be here, lets tell the view to be generated;
    4. Here you go view, you list them as you wish, I dont really care;
    

    Hope it helps!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比