Updated 1 December 2018
Today we are going to create a Post API in symfony. Here I assume that you have a active symfony project thats already connected to database if you don’t have a project create one and connect it to a database by either creating a new one or connecting your project to existing database. So lets start with our project.
Firstly create a new controller in your bundle as I did with the name NewController.php and create a fuction in it with any name but having suffix as ‘Action’ as
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
namespace AppBundle\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; class NewController extends Controller { /** * @Route("/api/create") * @Method("POST") */ public function createUserAction(Request $request) { return new Response('Method Created'); } } |
As you have seen we decided the route (or you can say URL) over here only using annotations. Also we have mentioned the method as POST that meams the method will be called when the given route is called with the ‘POST’ method. We can check the response but in order to do that we have to start our server. So we are starting our server with the following comand
1 |
$ php app/console server:run |
Now as our server is started just hit the “http://127.0.0.1:8000/api/create” but you cant just hit the request in your server. You can use various addons for REST, there are many available just search for them on Google.
Thats our API ready but lets now create a roe in our database using it. Here we have a entity User already in my database with five fields
So you have to send these in your request as POST body. So we will catch all these values from request in our createuserAction() as
1 2 3 4 5 6 7 8 9 10 11 12 |
public function createUserAction(Request $request) { try { $name = $request->request->get ( 'name' ); $email = $request->request->get ( 'email' ); $age = $request->request->get ( 'age' ); $contact = $request->request->get ( 'phone' ); return new Response ( 'Method Created' ); } catch ( Exception $e ) { return new Response ( "Exception" ); } } |
So we have all the data we need to create a user so we are going to create a new User object and the will flush (commit) the object in our database and change the response as ‘USER IS CREATED’
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
public function createUserAction(Request $request) { try { $name = $request->request->get( 'name' ); $email = $request->request->get( 'email' ); $age = $request->request->get( 'age' ); $contact = $request->request->get( 'phone' ); $user = new User(); $user->setName( $name ); $user->setEmail( $email ); $user->setAge( $age ); $user->setContact( $contact ); $doctrine_manager = $this->getDoctrine()->getManager(); $doctrine_manager->persist( $user ); $doctrine_manager->flush(); return new Response( 'USER IS CREATED' ); } catch ( Exception $e ) { return new Response( "Exception" ); } } |
And here you go run that API and get your reponse. You can also check in your database the value will be added. So thats it!! Happy Coding!!
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.