This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use Stack Overflow.

Console: doctrine:generate:crud (problem with the 'd' part)

Discuss Symfony 2 bundles here

Console: doctrine:generate:crud (problem with the 'd' part)

by ViC2 » Wed Aug 13, 2014 3:24 pm

Hello everyone,
I generated the CRUD actions of one of my entities with:

$ php app/console generate:doctrine:crud --entity=SomeBundle:Some --route-prefix=some --with-write --format=yml --no-interaction

This generates the files:
  • controller SomeBundle/Controller/SomeController.php
  • form SomeBundle/Form/SomeType.php
  • views index.html.twig, new.html.twig, edit.html.twig and show.html.twig in SomeBundle/Resources/views/Some/
  • routing some.yml in SomeBundle/Resources/config/routing/

Now, I can create, show and update with no problem BUT when I try to delete an entity I get the next error:
No route found for "GET /admin/some/1/delete": Method Not Allowed (Allow: POST)
405 Method Not Allowed - MethodNotAllowedHttpException
1 linked Exception:
MethodNotAllowedException »


ERROR - Uncaught PHP Exception Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException: "No route found for "GET /admin/some/1/delete": Method Not Allowed (Allow: POST)" at C:\wamp\www\Project\app\cache\dev\classes.php line 1880


This is part of my code:
SomeBundle/Resources/config/routing/some.yml
Code: Select all
//...
some_delete:
    pattern:     /{id}/delete
    defaults: { _controller: "SomeBundle:Some:delete" }
    requirements: { _method: post }


SomeBundle\Controller
Code: Select all
    /**
     * Deletes Some entity.
     *
     */
    public function deleteAction(Request $request, $id)
    {
        $form = $this->createDeleteForm($id);
        $form->handleRequest($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $entity = $em->getRepository('SomeBundle:Some')->find($id);

            if (!$entity) {
                throw $this->createNotFoundException('Unable to find Some entity.');
            }

            $em->remove($entity);
            $em->flush();
        }

        return $this->redirect($this->generateUrl('some'));
    } 

    
/**
     * Creates a form to delete Some 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('some_delete', array('id' => $id)))
            ->setMethod('DELETE')
            ->add('submit', 'submit', array('label' => 'Delete'))
            ->getForm()
        ;
    } 


Thank you in advance.
Do you realize that the sun doesn't go down? It's just an illusion caused by the world spinning round...
User avatar
ViC2
Junior Member
 
Posts: 7
Joined: Mon Jul 14, 2014 7:31 pm
Location: Matanzas Bay

Re: Console: doctrine:generate:crud (problem with the 'd' pa

by tgalopin » Fri Aug 29, 2014 5:51 pm

You need to use the deletion page with a deletion form. In the CRUD generated, the button to delete an entity is in fact a submit button associated with a Symfony deletion form (it's made that way to have CRSF on deletion).

If you made your own views, you may have deleted the render of this form. You have two solutions :

- remove the "requirements: { _method: post }" in your routes configuration ;
- use the form ;

I would recommand you to use a form to have CRSF.
tgalopin
Junior Member
 
Posts: 3
Joined: Wed Aug 06, 2014 2:50 pm