No route found for PUT / api / users / id

I am trying to update a record in my mysql database by calling the put method from my sencha touch 2 frontend. I call this url / api / users / id, but I keep getting Symfony error:

No route found for "PUT /api/users/1 

This is what I have in the routing.yml file

 users: resource: "Acme\MainBundle\Controller\UsersController" prefix: /api type: rest 

Also, I have putUsersAction setting in my User * s * Controller

 public function putUsersAction($id, Request $request) { $values['birthdate'] = $request->get('birthdate'); $values['clubid'] = $request->get('clubid'); $em = $this->getDoctrine()->getEntityManager(); $user = $this->getDoctrine() ->getRepository('AcmeMainBundle:User') ->find($id); $club = $this->getDoctrine() ->getRepository('AcmeMainBundle:Club') ->find($values['clubid']); $user->setBirthdate($values['birthdate']); $user->addClub($club); $em->flush(); $view = View::create() ->setStatusCode(200) ->setData($user); return $this->get('fos_rest.view_handler')->handle($view); } 

Why does Symfony tell me that there is no PUT / api / users / id route?

EDIT 1: router: debug output

 [router] Current routes Name Method Pattern _wdt ANY /_wdt/{token} _profiler_search ANY /_profiler/search _profiler_purge ANY /_profiler/purge _profiler_info ANY /_profiler/info/{about} _profiler_import ANY /_profiler/import _profiler_export ANY /_profiler/export/{token}.txt _profiler_phpinfo ANY /_profiler/phpinfo _profiler_search_results ANY /_profiler/{token}/search/results _profiler ANY /_profiler/{token} _profiler_redirect ANY /_profiler/ _configurator_home ANY /_configurator/ _configurator_step ANY /_configurator/step/{index} _configurator_final ANY /_configurator/final get_users GET /api/users.{_format} post_users POST /api/users.{_format} get_clubs GET /api/clubs.{_format} post_clubs POST /api/clubs.{_format} put_users PUT /api/users.{_format} 
+4
source share
2 answers

First you need to debug your routing and see if the route is registered correctly. Your published routing does not have the right intent. It should read:

 user: resource: "Acme\MainBundle\Controller\UserController" prefix: /api type: rest 

After that, you can debug your routing using the console command:

 php app/console router:debug 

Alternatively, you can use grep (Unix) or findstr (Windows) to search for output for your route:

 php app/console router:debug | grep /api 

or

 php app/console router:debug | findstr /api 

Next, make sure the FOSRestBundle automatic routing works as expected to name the controller * User * s * Controller and the file User * s * Controller.php.

See: FOSRestBundle Documentation

Please note that you forgot to redirect ($ user) before painting and that you cannot call a flash on a user object, but on your EntityManager. See my example below.

You can significantly reduce your controller by using DependencyInjection, symfony2 ParamConverter , implicit resource name definition and @View annotation provided by FOSRestBundle.

Then your controller will read something like this:

 <?php namespace Acme\MainBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\Request; use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; use JMS\DiExtraBundle\Annotation as DI; use Acme\MainBundle\Entity\User; use FOS\RestBundle\Controller\Annotations\View; /** * @DI\Service */ class UserController { /** @DI\Inject("doctrine.orm.entity_manager") */ private $em; // ... /** * @View() */ public function putAction(User $user, Request $request) { $club = $this->em ->getRepository('AcmeMainBundle:Club') ->findOneById($request->get('clubid')); $user ->setBirthdate($request->get('birthdate') ->addClub($club); // you should add some validation here $this->em->persist($user); $this->em->flush(); return $user; } // ... } 

Explanations:

I used the JMSDiExtraBundle annotations. You need this kit to make them work.

Otherwise, you must declare your controller as a service and manually enter the EntityManager (for example, in your Resources / config / services.xml bundle) in the service container.

Declare your controller as a Service with the annotation @DI \ Service.

Add your EntityManager here to be able to access it throughout the class using $ this-> em with @DI \ Inject annotation.

Use FOSRest @View annotation. Remember to set sensio_framework_extra.view: {annotations: false} before using this if you have SensioFrameworkExtraBundle in your application.

Make sure you return $ this; at the end of your User object setBirthdate (...) and addClub (...).

Please, not that I used the [JMSDiExtraBundle injection property] [3] in the example. The package must be installed for use.

You may be able to smooth the controller further using the NoxLogicMultiParamBundle.

I cannot post more than two links because im new here ...

  • Search the following resources on Google:
  • NoxLogicMultiParamBundle
  • FOSRestBundle Documentation
  • JMSDiExtraBundle Documentation
+7
source
 user: pattern: /api/user/{id} defaults: { _controller: AcmeMainBundle:User:putUsers, id: 1 } 

or you can use FQCN:

 defaults: { _controller: Acme\MainBundle\Controller\UserController::putUsersAction, id: 1 } 

and to match only the PUT request, use the code below, although some browsers do not support PUT and DELETE, see this link

 user: pattern: /api/user/{id} defaults: { _controller: Acme\MainBundle\Controller\UserController::putUsers, id: 1 } requirements: _method: PUT 

... and, of course, to see all your routes, run this from the project folder:

 php app/console router:debug 
0
source

Source: https://habr.com/ru/post/1446695/


All Articles