简体   繁体   中英

How to acces value from route in symfony2

I have some newbie question. How can i access specific value from my Route. This is my code:

/**
 * Catch controller.
 *
 * @Route("catch")
 */
class CatchController extends Controller
{
    /**
     * @Route("/{id}")
     * @Method({"GET"})
     * @ParamConverter("advertisement", class="AffiliateBundle:Advertisement")
     */
    public function goAction(Advertisement $advertisement)
    {
        //$advertisementId = $advertisement->getId();
        //die($advertisementId);
        //Creating new Lead
        $elo = $advertisement->getId();
        die($elo);
        $lead = new Lead();
        $lead ->setCreatedAt(new \DateTime());
        $lead ->setUpdatedAt(new \DateTime());
        //$lead ->setAdvertisement($advertisementId);
        $add = $this->getDoctrine()->getManager();
        $add->persist($lead);
        $add->flush();            
        //Taking id of the created lead
        $leadId = $lead->getId();
        //Saving cookie in user's browser
        $cookieValue = array(
            'name' => 'leadcookie',
            'value' => $leadId
        );
        $cookieLead = new Cookie($cookieValue['name'], $cookieValue['value']);
        $response = new Response();
        $response->headers->setCookie($cookieLead);
        //Redirecting user to advertisement url.
        $advertisementUrl = $advertisement->getUrlPattern();
        return $this->redirect($advertisementUrl);

    }

And when I call in my browser for example affiliate/catch/3 I can take every property from my Advertisement entity by it's methods. Like UrlPattern, RRSO, etc. But i need to take ID of this advertisement and when i use getId() in my controller instead of 3 I'm getting nothing. How should i do it?

You can try this :

/**
 * @Route("/{id}", requirements={"id" = "\d+"})
 * @Method({"GET"})
 * @ParamConverter("advertisement", class="AffiliateBundle:Advertisement")
 */
public function goAction(Advertisement $advertisement, $id) //add $id here
{
    //your id is stocked in $id, do what you want with it
    //I had "requirements" as your id must be a number
    //...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM