BookmarkController.php
This handles the storing and retrieving of JSON bookmark data associated with a user's ID.
storeBookmark
storeBookmarkpublic function storeBookmark(Request $request)
{
// Get the JSON data from the request
$jsonData = $request->input('jsonData');
// Decode the JSON data into an associative array
$jsonData = json_decode($jsonData, true);
// Specify the directory where bookmark files will be stored
$directory = 'Bookmark';
// Construct the file path based on the user's ID
$fileBookmark = $directory . '/' . $jsonData['USER_ID'] . '.json';
// Convert the array back to pretty-printed JSON format
$prettyPrintedJson = json_encode($jsonData, JSON_PRETTY_PRINT);
// Open the file for writing
$file = fopen($fileBookmark, 'w');
// Check if the file was successfully opened
if ($file) {
// Write the pretty-printed JSON data to the file
fwrite($file, $prettyPrintedJson);
// Close the file
fclose($file);
}
// Prepare a success response
$response = [
'SUCCESS' => 1,
];
// Return the response
return $response;
}To explain what this storeBookmark method does:
Receive Request Data:
The function receives a request object (
$request) which is an instance of theIlluminate\Http\Requestclass.
Extract JSON Data:
Extracts the JSON data from the request using
$request->input('jsonData').
Decode JSON:
Decodes the JSON data into an associative array using
json_decode($jsonData, true).
Specify Directory:
Specifies a directory named 'Bookmark' where bookmark files will be stored.
Construct File Path:
Constructs the file path based on the user's ID obtained from the JSON data.
Convert to Pretty-Printed JSON:
Converts the associative array back to pretty-printed JSON format using
json_encode($jsonData, JSON_PRETTY_PRINT).
Open File for Writing:
Opens a file for writing using
fopen($fileBookmark, 'w').
Check File Opening:
Checks if the file was successfully opened.
Write to File:
If the file is open, writes the pretty-printed JSON data to the file using
fwrite($file, $prettyPrintedJson).
Close File:
Closes the file using
fclose($file).
Prepare Success Response:
Prepares a success response array with the key
'SUCCESS'set to1.
Return Response:
Returns the success response array as the response for the function.
getBookmark
getBookmarkpublic function getBookmark(Request $request)
{
// Get the user ID from the request
$userId = $request->input('userId');
// Specify the directory where bookmark files are stored
$directory = 'Bookmark';
// Construct the file path based on the user's ID
$fileBookmark = $directory . '/' . $userId . '.json';
// Read the content of the bookmark file
$jsonContent = file_get_contents($fileBookmark);
// Decode the JSON content into an associative array
$jsonData = json_decode($jsonContent, true);
// Prepare a response with success status and bookmark data
$response = [
'SUCCESS' => 1,
'DATA' => $jsonData['DATA']
];
// Return the response
return $response;
}To explain what this getBookmark method does:
Receive Request Data:
The function receives a request object (
$request) which is an instance of theIlluminate\Http\Requestclass.
Extract User ID:
Extracts the user ID from the request using
$request->input('userId').
Specify Directory:
Specifies a directory named 'Bookmark' where bookmark files are stored.
Construct File Path:
Constructs the file path based on the user's ID obtained from the request.
Read File Content:
Reads the content of the bookmark file using
file_get_contents($fileBookmark).
Decode JSON:
Decodes the JSON content into an associative array using
json_decode($jsonContent, true).
Prepare Response:
Prepares a response array with the key
'SUCCESS'set to1and includes the bookmark data from the decoded JSON using$jsonData['DATA'].
Return Response:
Returns the response array as the response for the function.
Last updated