Skip to content

Instantly share code, notes, and snippets.

@mockiemockiz
Created November 22, 2014 17:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mockiemockiz/27008fb4b56c84096d2e to your computer and use it in GitHub Desktop.
Save mockiemockiz/27008fb4b56c84096d2e to your computer and use it in GitHub Desktop.
<?php
namespace Mockizart\Bundle\BlogBundle\Controller;
use Mockizart\Bundle\BlogBundle\MockizartBlogBundle;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Mockizart\Bundle\BlogBundle\Entity\MockblogTag;
use Mockizart\Bundle\BlogBundle\Form\MockblogTagType;
/**
* MockblogTag controller.
*
*/
class MockblogTagController extends Controller
{
protected $bundle;
public function __construct()
{
$this->bundle = new MockizartBlogBundle();
}
/**
* Lists all MockblogTag entities.
*
*/
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository($this->bundle->getTagEntityName())->findAll();
return $this->render($this->bundle->getTagEntityName() . ':index.html.twig', array(
'entities' => $entities,
));
}
/**
* Creates a new MockblogTag entity.
*
*/
public function createAction(Request $request)
{
$entity = new MockblogTag();
$entity->setDateCreated(time());
$entity->setUserId($this->getUser()->getId());
$form = $this->createCreateForm($entity);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('tag_show', array('id' => $entity->getId())));
}
return $this->render($this->bundle->getTagEntityName() . ':new.html.twig', array(
'entity' => $entity,
'form' => $form->createView(),
));
}
/**
* Creates a form to create a MockblogTag entity.
*
* @param MockblogTag $entity The entity
*
* @return \Symfony\Component\Form\Form The form
*/
private function createCreateForm(MockblogTag $entity)
{
$form = $this->createForm(new MockblogTagType(), $entity, array(
'action' => $this->generateUrl('tag_create'),
'method' => 'POST',
'attr' => ['class' => 'form-box'],
));
$form->add('submit', 'submit', array('label' => 'Create'));
return $form;
}
/**
* Displays a form to create a new MockblogTag entity.
*
*/
public function newAction()
{
$entity = new MockblogTag();
$form = $this->createCreateForm($entity);
return $this->render($this->bundle->getTagEntityName() . ':new.html.twig', array(
'entity' => $entity,
'form' => $form->createView(),
));
}
/**
* Finds and displays a MockblogTag entity.
*
*/
public function showAction($id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository($this->bundle->getTagEntityName())->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find MockblogTag entity.');
}
$deleteForm = $this->createDeleteForm($id);
return $this->render($this->bundle->getTagEntityName() . ':show.html.twig', array(
'entity' => $entity,
'delete_form' => $deleteForm->createView(),
));
}
/**
* Displays a form to edit an existing MockblogTag entity.
*
*/
public function editAction($id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository($this->bundle->getTagEntityName())->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find MockblogTag entity.');
}
$editForm = $this->createEditForm($entity);
$deleteForm = $this->createDeleteForm($id);
return $this->render($this->bundle->getTagEntityName() . ':edit.html.twig', array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
));
}
/**
* Creates a form to edit a MockblogTag entity.
*
* @param MockblogTag $entity The entity
*
* @return \Symfony\Component\Form\Form The form
*/
private function createEditForm(MockblogTag $entity)
{
$form = $this->createForm(new MockblogTagType(), $entity, array(
'action' => $this->generateUrl('tag_update', array('id' => $entity->getId())),
'method' => 'PUT',
));
$form->add('submit', 'submit', array('label' => 'Update'));
return $form;
}
/**
* Edits an existing MockblogTag entity.
*
*/
public function updateAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository($this->bundle->getTagEntityName())->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find MockblogTag entity.');
}
$deleteForm = $this->createDeleteForm($id);
$editForm = $this->createEditForm($entity);
$editForm->handleRequest($request);
if ($editForm->isValid()) {
$em->flush();
return $this->redirect($this->generateUrl('tag_edit', array('id' => $id)));
}
return $this->render($this->bundle->getTagEntityName() . ':edit.html.twig', array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
));
}
/**
* Deletes a MockblogTag entity.
*
*/
public function deleteAction(Request $request, $id)
{
$form = $this->createDeleteForm($id);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository($this->bundle->getTagEntityName())->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find MockblogTag entity.');
}
$em->remove($entity);
$em->flush();
}
return $this->redirect($this->generateUrl('tag'));
}
/**
* Creates a form to delete a MockblogTag entity by id.
*
* @param mixed $id The entity id
*
* @return \Symfony\Component\Form\Form The form
*/
private function createDeleteForm($id)
{
return $this->createFormBuilder()
->setAction($this->generateUrl('tag_delete', array('id' => $id)))
->setMethod('DELETE')
->add('submit', 'submit', array('label' => 'Delete'))
->getForm()
;
}
}
<?php
namespace Mockizart\Bundle\BlogBundle\Form;
use Mockizart\Bundle\BlogBundle\MockizartBlogBundle;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class MockblogTagType extends AbstractType
{
protected $bundle;
public function __construct()
{
$this->bundle = new MockizartBlogBundle();
}
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('slug')
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => $this->bundle->getTagEntityNamespace()
));
}
/**
* @return string
*/
public function getName()
{
return 'mockizart_bundle_blogbundle_mockblogtag';
}
}
{% extends '::base.html.twig' %}
{% block body -%}
<h1>MockblogTag creation</h1>
{{ form(form) }}
<ul class="record_actions">
<li>
<a href="{{ path('tag') }}">
Back to the list
</a>
</li>
</ul>
{% endblock %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment