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; class UserController { private $em;
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