For my Symfony2 project, I’d like to stick with the Fat Model Skinny Controller approach. I’ve got things working, however, there’s a lot of duplication / unnecessary business logic in my controller. What’s the best approach for cleaning things up?
The Controller
<?php
namespace TestApp\PeopleBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class PeopleController extends Controller {
public function indexAction() {
return $this->render('TestAppPeopleBundle:People:index.html.twig');
}
public function loadAction() {
$repository = $this->getDoctrine()->getRepository('TestAppPeopleBundle:People');
$request = $this->getRequest();
$parsedFilters = array();
$extFilterParser = $this->get('ext_filter_parser');
$filters = $request->query->get('filter');
if(empty($filters) === FALSE) {
$parsedFilters = $extFilterParser->setFilters($filters)->parse()->getParsedFilters();
}
$format = $request->getRequestFormat();
$people = $repository->getListOfPeople($parsedFilters);
$data = array('success' => true, 'people' => $people);
return $this->render('::base.'.$format.'.twig', array('data' => $data));
}
public function exportGridAction($format) {
$repository = $this->getDoctrine()->getRepository('TestAppPeopleBundle:People');
$request = $this->getRequest();
$parsedFilters = array();
$extFilterParser = $this->get('ext_filter_parser');
$filters = $request->query->get('filter');
if(empty($filters) === FALSE) {
$parsedFilters = $extFilterParser->setFilters($filters)->parse()->getParsedFilters();
}
$format = $request->getRequestFormat();
$people = $repository->getListOfPeople($parsedFilters);
$grid = $request->get('grid');
return $this->render('::extgrid.'.$format.'.twig', array('grid' => $grid, 'data' => $people));
}
}
The Entity Repository (People)
<?php
namespace TestApp\PeopleBundle\Repository;
use Doctrine\ORM\EntityRepository;
/**
* PeopleRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class PeopleRepository extends EntityRepository {
public function getQbForListOfPeople(array $filters) {
$queryBuilder = $this->createQueryBuilder('p');
$queryBuilder->select('p.id', 'p.firstName', 'p.lastName', 'p.dateOfBirth', 'p.address', 'p.city', 'p.state', 'p.zipCode');
foreach($filters as $filter) {
$queryBuilder->andWhere('p.' . $filter['expression'] . ' ' . $filter['value']);
}
$queryBuilder->setMaxResults(50);
return $queryBuilder;
}
public function getListOfPeople() {
return $this->getQbForListOfPeople(array())->getQuery()->getResult();
}
}
-
My thought was to put the logic for parsing filters out of the $_GET or $_POST inside the repository, but the repository doesn’t have access to the Request Object like the controller does.
-
My second thought was to put the logic for parsing filters out of the $_GET or $_POST inside the ExtFilterParser Service, but the service doesn’t have access to the Request Object like the controller does.
I would do your second thought and inject
@request(see my answer with code on this question – How to inject the @request into a service? ). This approach let you move away this code from controller into service:Second,
$requestwill be populated with Request object without having to specify$request = $this->getRequest();if you use this: