我有一些新手问题。如何从我的路线访问特定值。 这是我的代码:
/**
* 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);
}
当我在浏览器中调用例如affiliate/catch/3
时,我可以通过它的方法从我的广告实体中获取每个属性。像UrlPattern,RRSO等
但我需要获取此广告的ID,当我在我的控制器中使用getId()
而不是3
时,我什么都没得到。我该怎么办?
答案 0 :(得分:0)
你可以试试这个:
/**
* @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
//...
}