Created
August 15, 2017 15:09
-
-
Save anonymous/9b78a9e840c034cb195c103db489d7e5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
////////////////////////////////////////////// | |
services | |
////////////////////////////////////////////// | |
parameters: | |
class: Home\PageBundle\Entity\Box | |
services: | |
box_form: | |
class: Symfony\Component\Form\Form | |
factory: ['@form.factory', create ] | |
box_handler: | |
class: Home\PageBundle\Handler\BoxHandler | |
arguments: | |
- "@box_form" | |
- "$request" | |
////////////////////////////////////////////// | |
controller | |
////////////////////////////////////////////// | |
/** | |
* Creates a new box entity. | |
* | |
* @Route("/new", name="box_new") | |
* @Method({"GET", "POST"}) | |
*/ | |
public function newAction(RequestStack $requestStack) | |
{ | |
$box = new Box(); | |
$form = $this->get('box_form'); | |
$boxhandler = $this->get('box_handler'); | |
$requestStack->getCurrentRequest() | |
if ($boxhandler->process()) { | |
$em = $this->getDoctrine()->getManager(); | |
$em->persist($box); | |
$em->flush(); | |
return $this->redirectToRoute('box_show', array('id' => $box->getId())); | |
} | |
return $this->render('box/new.html.twig', array( | |
'box' => $box, | |
'form' => $boxhandler->getForm()->createView(), | |
)); | |
} | |
////////////////////////////////////////////// | |
handler | |
////////////////////////////////////////////// | |
<?php | |
namespace Home\PageBundle\Handler; | |
use Symfony\Component\Form\Form; | |
use Symfony\Component\HttpFoundation\Request; | |
class BoxHandler | |
{ | |
protected $form; | |
protected $request; | |
public function __construct(Form $form, Request $request) | |
{ | |
$this->form = $form; | |
$this->request = $request; | |
} | |
public function process() | |
{ | |
$this->form->handleRequest($this->request); | |
if ($this->form->isSubmitted() && $this->form->isValid()) | |
{ | |
return true; | |
} | |
return false; | |
} | |
public function onsucess() | |
{ | |
} | |
public function getForm() | |
{ | |
return $this->form; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment