【问题标题】:In Symfony2, where should I put that function used by my Controller?在 Symfony2 中,我应该把控制器使用的那个函数放在哪里?
【发布时间】:2012-06-27 23:47:48
【问题描述】:

假设Human 的口袋里可以有Item(s)。每个ItemHuman有不同的影响。

当他使用一个物品时,它会转到ItemController

class ItemController extends Controller
{
    public function useAction() {
       // Human
        $human = new Human();

       // Item
        $request = Request::createFromGlobals();
        $item_id = $request->request->get('item_id', 0);
        $item = new Item($item_id);

        // Different effects depending on the Item used
        switch($item->getArticleId()) {

            case 1: $this->heal($human, $item, 5); break; // small potion
            case 2: $this->heal($human, $item, 10); break; // medium potion
            case 3: $this->heal($human, $item, 15); break; // big potion

        }

    }

    // The following Heal Function can be placed here ?
    private function heal($human, $item, $life_points) {
        $human->addLife($life_points);
        $item->remove();
        return new Response("You have been healed of $life_points");
    }
}

heal function 可以放在这里吗?我认为它不应该在 Controller 中。但我也认为它不应该放在Item Entity 内(因为响应,并且因为它使用$Human)

【问题讨论】:

  • 您上面的代码是否只是可能发生的事情的一个示例,而不是您使用的实际代码?

标签: php symfony controller


【解决方案1】:

这取决于。我对这类问题的推理是:如果我只使用控制器中的功能,它可以留在那里。但如果它可能是一个共享功能,我将为它创建一个服务。也许您希望能够通过命令或不同的控制器或其他东西来治愈人类。在这种情况下,为此使用共享代码是有意义的。

Creating a service 非常简单,可以让您将逻辑保存在共享位置。在我看来,控制器对于处理请求流更有用。

【讨论】:

  • 问题不是陈述,如果该功能在控制器中使用很多,而在另一个控制器中使用一次或两次,那么在另一个控制器中实例化项目控制器是一个坏主意,设置容器并调用愈合功能? Heal 当然必须公开
  • 绝对不是,如果它只用于控制器,我会使用继承,就像@smoreno 的回答所暗示的那样。你描述它的方式,你正在做我描述的完全相同的事情,但是使用了错误的组件来做到这一点。
【解决方案2】:

我认为你可以这样做:

1:继承

  class BaseHumanController extend Controller{

  public function heal($param1, $param2, $param3){

  //put logic here

  }

} 

//Extend from BaseHumanController in any controller for call heal() method
class ItemController extend BaseHumanController{

//....
 $this->heal(...)

} 

2:使用您的heal() 方法创建一个类,并将其配置为@Peter Kruithof 的服务

【讨论】:

    【解决方案3】:

    我绝对认为它会进入控制器。来自维基百科:

    控制器调解输入,将其转换为模型的命令 或查看。

    如果您查看 symfony2 crud 生成器生成的删除函数,它会在实体上调用 remove:

    /**
     * Deletes a Bar entity.
     *
     * @Route("/{id}/delete", name="bar_delete")
     * @Method("post")
     */
    public function deleteAction($id)
    {
        $form = $this->createDeleteForm($id);
        $request = $this->getRequest();
    
        $form->bindRequest($request);
    
        if ($form->isValid()) {
            $em = $this->getDoctrine()->getEntityManager();
            $entity = $em->getRepository('FooBundle:Bar')->find($id);
    
            if (!$entity) {
                throw $this->createNotFoundException('Unable to find Bar entity.');
            }
    
            $em->remove($entity);
            $em->flush();
        }
    
        return $this->redirect($this->generateUrl('bar_index'));
    }
    

    【讨论】:

      【解决方案4】:

      我看不出它为什么不应该在控制器中。如果您只想在那里使用它,只需将其设为私有方法即可。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-30
        • 1970-01-01
        • 2016-08-25
        • 2013-08-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多