How To Create Api in Symfony 4 using JSON data ?
Here, I will show you how to create Api in symfony for beginners.
Step 1:
Navigate to project directory & run below command for create fresh project.
composer create-project symfony/skeleton [project-name]
Step 2:
Now Navigate to project path & Create controller via below command.
php bin/console make:controller
Step 3:
Open that controller, now we need to create three methods for api, first of all we look into listing method which make the list of given JSON array [ index() ]
/**
* @Route(“/api/user”, name=”api_user”)
*/
public function index(Request $request)
{
//initilize array
$data = array();
$userArr = array();
// make api request
$request_user_url = ‘../users.json’;
$request_user_result = file_get_contents( $request_user_url );
$user_details = json_decode( $request_user_result, true );
foreach ($user_details as $user)
{
$userArr[‘id’] = $user[‘id’];
$userArr[‘name’] = $user[‘name’];
$userArr[‘date’] = $user[‘date’];
$userArr[‘address’] = $user[‘address’];
$userArr[‘last_modified’] = $user[‘last_modified’];
array_push($data, $userArr);
}
return new JsonResponse(array(‘data’ => $data, ‘count’ => count($data)));
}
Step 4:
Second method for update the JSON data [ update() ]
/** * @Route(“/api/user-update/{id}/{name}/{date}/{address}”, name=”api_user_update”) */
public function update( Request $request, $name = null, $date = null, $address = null ) {
$status = ‘error’;
$message = ‘User not found!’;
$id = $request->get(‘id’);
$name = $request->get(‘name’);
$date = $request->get(‘date’);
$address = $request->get(‘address’);
// make api request
$request_user_url = ‘../users.json’;
$request_user_result = file_get_contents( $request_user_url );
$user_details = json_decode( $request_user_result, true );
foreach ( $user_details as $key => $value ) {
if( $value[‘id’] == $id ) {
if( $name != null ) {
$user_details[$key][‘name’] = $name;
}
if( $date != null) {
$user_details[$key][‘date’] = $date;
}
if( $address != null ) {
$user_details[$key][‘address’] = $address;
}
$user_details[$key][‘last_modified’] = date(‘Y-m-d H:i:s’);
// updates the specific user..
$status = ‘success’;
$message = ‘User details update successfully!’;
}
}
// encode array to json and save to file
file_put_contents($request_user_url, json_encode($user_details));
return new JsonResponse(array(‘status’ => $status, ‘message’ => $message));
}
Step 5:
Third method for delete the JSON data [ delete() ]
/** * @Route(“/api/user-delete/{id}”, name=”api_user_delete”) */
public function delete(Request $request) {
$status = ‘error’;
$message = ‘User not found!’;
$userId = $request->get(‘id’);
// make api request
$request_user_url = ‘../users.json’;
$request_user_result = file_get_contents( $request_user_url );
$user_details = json_decode( $request_user_result, true );
foreach ( $user_details as $key => $value )
{
if( $value[‘id’] == $userId ) {
// delete specific user..
unset($user_details[$key]); $status = ‘success’; $message = ‘User deleted successfully!’;
}
}
// encode array to json and save to file
file_put_contents($request_user_url, json_encode($user_details));
return new JsonResponse(array(‘status’ => $status, ‘message’ => $message));
}
That’s all, thanks for reading our blog, hope this will help a lot to you.