2017 © Pedro Peláez
 

bundle extra-bundle

Utility services and classes for Symfony projects

image

javihgil/extra-bundle

Utility services and classes for Symfony projects

  • Wednesday, January 31, 2018
  • by javihgil
  • Repository
  • 1 Watchers
  • 1 Stars
  • 93 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

Extra Bundle

Utility services and classes for Symfony projects, (*1)

Installation

composer require javihgil/extra-bundle ~1.0

Configure Bundle

Register the bundle in app/AppKernel.php:, (*2)

// app/AppKernel.php
public function registerBundles()
{
    return [
        // ...
        new \Jhg\ExtraBundle\ExtraBundle(),
    ];
}

Controller extension

To use the utilities in ExtraController extend your controllers from Jhg\ExtraBundle\Controller\ExtraController instead of Symfony FrameworkBundle one., (*3)

<?php 
namespace AppBundle\Controller;

use Jhg\ExtraBundle\Controller\ExtraController;

class MyController extends ExtraController
{

}

Some usefull methods:, (*4)

getRepository, (*5)

$this->getRepository('AppBundle:User')->findBy(...)

trans, (*6)

$this->trans('Some translatable text');

transChoice, (*7)

$this->transChoice('{0} There are no apples|{1} There is one apple|]1,Inf[ There are %count% apples', $count);

addFlashTrans, (*8)

$this->addFlashTrans('error', 'A translatable error message');

Form handlers

Form handler interface

The mission of Form Handlers is to encapsulate CrUD business operations into one proccessor class., (*9)

All the forms must be validated, then some action must be done with contained data, and usually those actions are duplicated., (*10)

With the use of form handlers all those actions are in one method, and can be called anywhere., (*11)

Doctrine Create form handler

Handler for creating doctrine entities in a CRUD controller., (*12)

Example, (*13)

/**
 * @param Request $request
 *
 * @return Response
 */
public function createAction(Request $request)
{
    $element = new Element();

    $form = $this->createForm(new ElementType(), $element);

    if ($this->get('create_form_handler')->process($form, $request)) {
        return $this->redirectToRoute('success_route', ['element', $element->getId()]);
    }

    $viewData = [
        'form' => $form->createView(),
    ];

    return $this->render('ExampleBundle:Element:create.html.twig', $viewData);
}

Doctrine Update form handler

Handler for updating doctrine entities in a CRUD controller., (*14)

Example, (*15)

/**
 * @param Element $element
 * @param Request $request
 *
 * @return Response
 */
public function updateAction(Element $element, Request $request)
{
    $form = $this->createForm(new ElementType(), $element);

    if ($this->get('update_form_handler')->process($form, $request)) {
        return $this->redirectToRoute('success_route', ['element', $element->getId()]);
    }

    $viewData = [
        'element' => $element,
        'form' => $form->createView(),
    ];

    return $this->render('ExampleBundle:Element:update.html.twig', $viewData);
}

Doctrine Delete form handler

Handler for deleting doctrine entities in a CRUD controller., (*16)

Example, (*17)

/**
 * @param Element $element
 * @param Request $request
 *
 * @return Response
 */
public function deleteAction(Element $element, Request $request)
{
    $form = $this->createForm(new ElementType(), $element);

    if ($this->get('delete_form_handler')->process($form, $request)) {
        return $this->redirectToRoute('success_route');
    }

    $viewData = [
        'element' => $element,
        'form' => $form->createView(),
    ];

    return $this->render('ExampleBundle:Element:delete.html.twig', $viewData);
}

The Versions