I’m trying to update a record in my mysql database by calling the put method from my sencha touch 2 frontend. I’m calling this url /api/users/id but I keep getting a Symfony error:
No route found for "PUT /api/users/1
This is what I have in my routing.yml file
users:
resource: "Acme\MainBundle\Controller\UsersController"
prefix: /api
type: rest
Also, I have the putUsersAction setup 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 is Symfony telling me there’s 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}
At first you should debug your routing and see if the route is being registered correctly. Your posted routing snipped lacks correct intendation. It should read:
Afterwards you can debug your routing with the console command:
Furthermore you can use grep ( Unix ) or findstr ( Windows ) to search the output for your route:
or
Next make sure for the automatic routing of FOSRestBundle to work as expected to name your controller User*s*Controller and the file User*s*Controller.php.
See: FOSRestBundle Documentation
Please note that you have forgotten to call persist($user) before flushing and that you cannot call flush on your User entity but on your EntityManager. See my example below.
You can slim down your controller a lot by using DependencyInjection , the symfony2 ParamConverter, implicit resource name definition and the @View Annotation provided by the FOSRestBundle.
Your Controller would then read something like this:
Explanations:
I have used the JMSDiExtraBundle’s annotations. You need this bundle to make them work.
Otherwise you should declare your controller as a service and inject the EntityManager manually ( for example in your bundle’s Resources/config/services.xml ) in the service container.
Declare your controller as Service with @DI\Service annotation.
Inject your EntityManager here to be be able to access it throughout the class with $this->em with the @DI\Inject annotation.
Use FOSRest’s @View annotation.
Don’t forget to set sensio_framework_extra.view: { annotations: false } before using this if you have the SensioFrameworkExtraBundle’s in your application.
Make sure you have return $this; at the end of your User entity’s setBirthdate(…) and addClub(…) functions.
Please not that i have used [JMSDiExtraBundle’s property injection][3] in the example.
The bundle has to be installed in order to be used.
You might be able to slim down the controller further by using the NoxLogicMultiParamBundle.
I cannot post more than 2 links because im new over here…