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 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行