dtu36380 2017-03-10 21:33
浏览 18
已采纳

Symfony 2 - 减少冗余代码的方法

I'm writing a simple Symfony2 web application. I realised that almost every controller in my application does the same operations, but on a different object. I'm talkin about standard CRUD operations: creating, reading, updating and deleting. I will use 'delete' operation as an example

Example:

class CustomerController {
/**
 * @Route("/customer")
 * @Method("DELETE")
 */
public function deleteAction(Request $request){
    $rep =this->getDoctrine()->getManager()->getRepository('AppBundle:Customer');

    $customerId = $request->get('id');
    if ($customerId != null) {
        $rep->delete($customerId);
    } else {
        $rep->deleteAll();
    }
    $response = new Response(null, Response::HTTP_OK);
    return $response;
}
}

class ProductController {
/**
 * @Route("/product")
 * @Method("DELETE")
 */
public function deleteAction(Request $request){
    $rep =this->getDoctrine()->getManager()->getRepository('AppBundle:Product');

    $productId = $request->get('id');
    if ($productId != null) {
        $rep->delete($productId);
    } else {
        $rep->deleteAll();
    }
    $response = new Response(null, Response::HTTP_OK);
    return $response;
}
}

class CompanyController {
/**
 * @Route("/company")
 * @Method("DELETE")
 */
public function deleteAction(Request $request){
    $rep =this->getDoctrine()->getManager()->getRepository('AppBundle:Company');

    $companyId = $request->get('id');
    if ($companyId != null) {
        $rep->delete($companyId);
    } else {
        $rep->deleteAll();
    }
    $response = new Response(null, Response::HTTP_OK);
    return $response;
}
}

and so on ...

The only thing that really changes is the entity name ("Customer", "Product", "Company", etc...).

Do you have any ideas how to get rid off all this redundancy and at the same time keep the code readable ?

The first thing that comes to my mind is to create a base class with the delete method logic and just pass entity name as a parameter. Is it ok to do so? For example:

class CustomerController extends BaseController{
/**
 * @Route("/customer")
 * @Method("DELETE")
 */
public function deleteAction(Request $request){
    parent::deleteAction($request, 'AppBundle:Customer');
}

Is the above legit solution ? Are there ways to simplify it further ?

  • 写回答

1条回答 默认 最新

  • dse3168 2017-03-11 00:13
    关注

    I would use a single controller. Use the route to get the entity classname.

    /**
     * @Route("/{entityName}/{entityId}")
     * @Route("/{entityName}")
     * @Method("DELETE")
     */
    public function deleteAction($entityName, $entityId = null){
        $rep =this->getDoctrine()->getManager()->getRepository('AppBundle:'.$entityName);
        if ($productId != null) {
            $rep->delete($productId);
        } else {
            $rep->deleteAll();
        }
        $response = new Response(null, Response::HTTP_OK);
        return $response;
    }
    }
    

    Depending on how you build your routes you should have to translate the $entityName string for instance: /entity-name to EntityName or /entity to Entity

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

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀