2016-03-29 14 views
0
Ben bir dinlenme api geliştirmeye çalışıyorum

iyi çalışıyor alır, ben, yolları ek açıklamaları ile ilgili bir sorun bilmiyorum yazı içinPOST rota açıklama Symfony2

/** 
* @QueryParam(name="offset", requirements="\d+", nullable=true, description="Offset from which to start listing pages.") 
* @QueryParam(name="limit", requirements="\d+", nullable=true, default="20", description="How many pages to return.") 
* 
* @View() 
* 
* @param Request    $request  the request object 
* @param ParamFetcherInterface $paramFetcher param fetcher service 
* GET Route annotation. 
* @Get("https://stackoverflow.com/users/") 
* @return array 
*/ 
public function getUsersAction(Request $request, ParamFetcherInterface $paramFetcher) 
{ 
    $offset = $paramFetcher->get('offset'); 
    $offset = null == $offset ? 0 : $offset; 
    $limit = $paramFetcher->get('limit'); 

    return $this->container->get('user.handler')->all($limit, $offset); 
} 

aşağıdaki gibi get benim kodudur aşağıdaki gibi bunu nasıl benim kod i url kullanıcının attribues eklemek istediğiniz, yani plz nasıl

  /** 
* @View(template = "UserBundle:User:register.html.twig", statusCode = Codes::HTTP_BAD_REQUEST, templateVar = "form") 
* 
* @param Request $request the request object 
* POST Route annotation. 
* @Post("https://stackoverflow.com/users/") 
* @return FormTypeInterface|View 
*/ 
public function postUserAction(Request $request) 
{ 
    try { 
     $newUser = $this->get('user.handler')->post($request); 

     $routeOptions = array(
       'id' => $newUser->getId(), 
       '_format' => $request->get('_format') 
     ); 

     return $this->routeRedirectView('get_user', $routeOptions, Codes::HTTP_CREATED); 
    } catch (InvalidFormException $exception) { 

     return $exception->getForm(); 
    } 
} 
+0

FOSRestBundle'ı kullandığınızdan bahsetmeyi unutmuşsunuzdur. Bu doğru mu? – Andras

+0

evet fosRestBundle – Ndb

cevap

0

Sen isteğinden bu parametreleri alabilirsiniz:

/** 
* @View(template = "UserBundle:User:register.html.twig", statusCode = Codes::HTTP_BAD_REQUEST, templateVar = "form") 
* 
* @param Request $request the request object 
* POST Route annotation. 
* @Route("/user/{name}/{email}") 
* @Method("POST") 
* @return FormTypeInterface|View 
*/ 
public function postUserAction(Request $request, $name, $email) 
{ 
    try { 

     $newUser = $this->get('user.handler')->post($request);   

     $routeOptions = array(
       'id' => $newUser->getId(), 
       '_format' => $request->get('_format') 
     ); 

     return $this->routeRedirectView('get_user', $routeOptions, Codes::HTTP_CREATED); 
    } catch (InvalidFormException $exception) { 

     return $exception->getForm(); 
    } 
} 
+0

kullanıyorum lütfen bunu nasıl yapabilirim? – Ndb

+0

benim durumumda, bir kullanıcının urm özniteliklerini eklemek için onu eklemek istiyorum .. user/fabian/[email protected]/.... – Ndb

+0

Yanıtı güncelledim, yönlendirici yer tutucuları ekleyebilirsiniz. Daha fazla bilgiyi http://symfony.com/doc/current/book/routing.html#routing-with-placeholders adresinde okuyun. Alse, gereksinimleri ve varsayılanları ekleyebilirsiniz: http://symfony.com/doc/current/book/routing.html#adding-http-method-requirements –