Controller Style Example

Version: 0.1.0 +

The "controller" style is probably the easies to use and understand, but the least flexible and testable.

Implementation

The basic concept of this style is to have two actions, one for the display of the table and one for the response of the data. Data is provided by the getQueryBuilder function based on the request provided and formatted within the data action. Rendering is done through the twig extension, and requires a url path to the data action.

Example Table

Id Name

Code

Controller

<?php
namespace Brown298\DtTestBundle\Controller;

use Brown298\DataTablesBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

/**
 * Class DtControllerStyleController
 *
 * @Route("/style/controller/")
 * @package Brown298\DtTestBundle\Controller
 */
class DtControllerStyleController extends AbstractController
{
    /**
     * @var array
     */
    protected $columns = array(
        'user.id'   => 'Id',
        'user.username' => 'Name',
    );

    /**
     * getQueryBuilder
     *
     * @param Request $request
     *
     * @return null
     */
    public function getQueryBuilder(Request $request)
    {
        $em             = $this->get('doctrine.orm.entity_manager');
        $userRepository = $em->getRepository('Brown298\DtTestBundle\Entity\User');
        $qb = $userRepository->createQueryBuilder('user')
            ->andWhere('user.deleted = false');

        return $qb;
    }

    /**
     * dataAction
     *
     * @route("/ajax", name="controller_style_ajax")
     *
     * @param Request $request
     * @param null $dataFormatter
     *
     * @return JsonResponse
     */
    public function dataAction(Request $request, $dataFormatter = null)
    {
        $renderer = $this->get('templating');

        return parent::dataAction($request, function($data) use ($renderer) {
            $count   = 0;
            $results = array();

            foreach ($data as $row) {
                $results[$count][] = $row['id'];
                $results[$count][] = $renderer->render('Brown298DtTestBundle:DtControllerStyle:nameFormatter.html.twig', array('name' => $row['username']));
                $count += 1;
            }

            return $results;
        });
    }

    /**
     * indexAction
     *
     * @route("", name="controller_style")
     * @Template()
     *
     * @return array
     */
    public function indexAction()
    {
        return array(
            'columns'       => $this->columns,
        );
    }
}

Twig Index (index.html.twig)

{% extends 'Brown298DtTestBundle::base.html.twig' %}

{% block body %}
    <h2>Controller Style Example</h2>

    {% include "Brown298DtTestBundle:DtControllerStyle:content.html.twig" %}

    <h4>Example Table</h4>
    <div class="table-bordered">
        {{ addDataTable(columns, {
        'path':          path('controller_style_ajax'),
        'bLengthChange': 1,
        'bInfo':         1,
        'bFilter':       1,
        }) }}

    </div>

{% endblock%}

Twig Formatter (nameFormatter.html.twig)

<span class='strong'>{{ name }} </span>