Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace D\UserBundle\Controller;
- use Symfony\Component\HttpFoundation\Request;
- use Symfony\Bundle\FrameworkBundle\Controller\Controller;
- use D\UserBundle\Entity\User;
- use D\UserBundle\Form\UserType;
- /**
- * User controller.
- *
- */
- class UserController extends Controller
- {
- /**
- * Lists all User entities.
- *
- */
- public function indexAction()
- {
- $em = $this->getDoctrine()->getManager();
- $entities = $em->getRepository('DUserBundle:User')->findAll();
- return $this->render('DUserBundle:User:index.html.twig', array(
- 'entities' => $entities,
- ));
- }
- /**
- * Finds and displays a User entity.
- *
- */
- public function showAction($id)
- {
- var_dump($_SESSION);
- $em = $this->getDoctrine()->getManager();
- $entity = $this->get('security.context')->getToken()->getUser();
- if (!$entity) {
- throw $this->createNotFoundException('Unable to find User entity.');
- }
- $deleteForm = $this->createDeleteForm($id);
- return $this->render('DUserBundle:User:show.html.twig', array(
- 'entity' => $entity,
- 'delete_form' => $deleteForm->createView(), ));
- }
- /**
- * Displays a form to create a new User entity.
- *
- */
- public function newAction()
- {
- $entity = new User();
- $form = $this->createForm(new UserType(), $entity);
- return $this->render('DUserBundle:User:new.html.twig', array(
- 'entity' => $entity,
- 'form' => $form->createView(),
- ));
- }
- /**
- * Creates a new User entity.
- *
- */
- public function createAction(Request $request)
- {
- $entity = new User();
- $form = $this->createForm(new UserType(), $entity);
- $form->bind($request);
- if ($form->isValid()) {
- $em = $this->getDoctrine()->getManager();
- $em->persist($entity);
- $em->flush();
- return $this->redirect($this->generateUrl('user_show', array('id' => $entity->getId())));
- }
- return $this->render('DUserBundle:User:new.html.twig', array(
- 'entity' => $entity,
- 'form' => $form->createView(),
- ));
- }
- /**
- * Displays a form to edit an existing User entity.
- *
- */
- public function editAction($id)
- {
- $em = $this->getDoctrine()->getManager();
- $entity = $em->getRepository('DUserBundle:User')->find($id);
- if (!$entity) {
- throw $this->createNotFoundException('Unable to find User entity.');
- }
- $editForm = $this->createForm(new UserType(), $entity);
- $deleteForm = $this->createDeleteForm($id);
- return $this->render('DUserBundle:User:edit.html.twig', array(
- 'entity' => $entity,
- 'edit_form' => $editForm->createView(),
- 'delete_form' => $deleteForm->createView(),
- ));
- }
- /**
- * Edits an existing User entity.
- *
- */
- public function updateAction(Request $request, $id)
- {
- $em = $this->getDoctrine()->getManager();
- $entity = $em->getRepository('DUserBundle:User')->find($id);
- if (!$entity) {
- throw $this->createNotFoundException('Unable to find User entity.');
- }
- $deleteForm = $this->createDeleteForm($id);
- $editForm = $this->createForm(new UserType(), $entity);
- $editForm->bind($request);
- if ($editForm->isValid()) {
- $em->persist($entity);
- $em->flush();
- return $this->redirect($this->generateUrl('user_edit', array('id' => $id)));
- }
- return $this->render('DUserBundle:User:edit.html.twig', array(
- 'entity' => $entity,
- 'edit_form' => $editForm->createView(),
- 'delete_form' => $deleteForm->createView(),
- ));
- }
- /**
- * Deletes a User entity.
- *
- */
- public function deleteAction(Request $request, $id)
- {
- $form = $this->createDeleteForm($id);
- $form->bind($request);
- if ($form->isValid()) {
- $em = $this->getDoctrine()->getManager();
- $entity = $em->getRepository('DUserBundle:User')->find($id);
- if (!$entity) {
- throw $this->createNotFoundException('Unable to find User entity.');
- }
- $em->remove($entity);
- $em->flush();
- }
- return $this->redirect($this->generateUrl('user'));
- }
- private function createDeleteForm($id)
- {
- return $this->createFormBuilder(array('id' => $id))
- ->add('id', 'hidden')
- ->getForm()
- ;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement