user3672853
user3672853

Reputation:

Send a post parameter with path

I have a list of demands, that I can remove some, so I have created a path for every demand like that:

<a href="{{ path('etudiant_suprimer_demande',{'id':demande.id} )}}

And I have a controller like that:

class EdudiantController extends Controller
{ //codes
  public function suprimerdemandeAction($id){

  //  echo $id;
    $em = $this->getDoctrine()
        ->getEntityManager();
    $demande = $em->getRepository('AcmeEstmSiteBundle:Demande')
        ->find($id);
    if ($demande == null) {
        throw $this->createNotFoundException('Demande[id='.$id.']inexistant');
    }

    if ($this->get('request')->getMethod() == 'POST') {
        $this->get('session')->getFlashBag()->add('info', 'Demande bien supprimée');
        return $this->redirect( $this->generateUrl('acme_estm_site_espace_etudiant'));

    }

    //return $this->render('SdzBlogBundle:Blog:supprimer.html.twig',array('demande' => $demande));
    return new Response();
}

And the routing is:

etudiant_suprimer_demande:
    path:     /profile/espace/suprimerdemande/{id}
    defaults: { _controller: AcmeEstmSiteBundle:Edudiant:suprimerdemande }
    methods: POST

What I want is do not show the id of demands in the URL, that means I want this action with a post method but I have this error:

No route found for "GET /profile/espace/suprimerdemande/3": Method Not Allowed (Allow: POST)

Upvotes: 1

Views: 3734

Answers (1)

pikachu0
pikachu0

Reputation: 812

TL;DR

<form action="{{ path('etudiant_suprimer_demande',{'id': demande.id} )}}" method="post">
  <button type="submit">Delete</button>
</form>

Explanation

The code you have now only generates an <a> link, which when a user clicks it, his user agent sends a GET request to the server, thus the error you get.

In order to create a POST request, you need a <form>.

To answer your question regarding showing ID in the generated URL, take a look at this answer. You'll have to change your controller and routing.

Upvotes: 1

Related Questions