简体   繁体   English

如何在symfony2中从路由访问值

[英]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. 当我在我的浏览器中调用例如affiliate/catch/3我可以通过它的方法从我的广告实体中获取每个属性。 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. 像UrlPattern,RRSO等。但我需要获取此广告的ID,当我在我的控制器中使用getId()而不是3我什么都没得到。 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
    //...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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