doushizhou4477 2017-09-14 11:35
浏览 23
已采纳

Symfony文档中的“控制器”和“操作”是什么?

I just discovered the official Symfony best practices about controllers, where it is written:

As a rule of thumb, you should follow the 5-10-20 rule, where controllers should only define 5 variables or less, contain 10 actions or less and include 20 lines of code or less in each action.

I have a question about its meaning, as it could be understood in two different ways.

  1. "controllers" designates the controller classes (ex class MyController{}) & "actions" designates the controller functions (ex public function indexAction(){}) => It makes no sense to me

  2. "controllers" designates the controller functions (ex public function indexAction(){}) & "actions" designates instructions in these functions (ex if(){//do something}) => It makes sense to me

In case 1, that means something like:

// Approximate maximum number of lines : 200    
class MyController {

    // Max 20 lines of code
    public function firstAction(){}

    [...]

    // Max 20 lines of code
    public function tenthAction(){}
}

Also, I can't get in this case what means "5 variables or less". 5 global variables? 5 local variables for all of the 10 Actions?

In case 2, that means something like:

// No maximum number of lines
class MyController {

    // Max 10 actions * 20 lines by actions = 200 lines of code, and 5 local variables
    public function firstAction(){}

    [...]

    // Max 10 actions * 20 lines by actions = 200 lines of code, and 5 local variables
    public function fiftiethAction(){}

}

The goal of my question is to be absolutely sure to understand the good meaning. But I also think that it would be useful to ask the Symfony team to rewrite this sentence without leaving any ambiguities? I specially think about non-English developers like I am, or Symfony beginners.

Considering that the case 2 is the good one, the word "action(s)" could be replaced with "instruction(s)" or "directive(s)", for example?

  • 写回答

1条回答 默认 最新

  • dongrouyuan5685 2017-09-14 11:43
    关注

    A Controller is a class that has actions in it. An action is a method that runs for a specific view.

    Meaning 1 you suggested is correct.

    What they recommend is that you keep the Controllers as simple as you can. An action method with loads of lines of code isn't as clean as an action method with only one little task to perform.

    class SomeController
    {
         private $variablesLikeThis;
         private $keepToAroundFive;
    
         public function someAction()
        {
            // no more than ten of these action methods
            // don't make the code in here too long (20 lines or so)
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有偿求码,CNN+LSTM实现单通道脑电信号EEG的睡眠分期评估
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路