Skip to content

Instantly share code, notes, and snippets.

@ImtiazEpu
Last active May 22, 2023 17:45
Show Gist options
  • Save ImtiazEpu/b285e591a9b85c222f86ae632debaa36 to your computer and use it in GitHub Desktop.
Save ImtiazEpu/b285e591a9b85c222f86ae632debaa36 to your computer and use it in GitHub Desktop.
<?php
//Question 1:
//You have a Laravel application with a form that submits user information using a POST request. Write the code to retrieve the 'name' input field value from the request and store it in a variable called $name.
//=============================
Route::post( '/submit', function ( Request $request ) {
// Retrieve the 'name' input field value from the request
$name = $request->input( 'name' );
} );
//Question 2:
//In your Laravel application, you want to retrieve the value of the 'User-Agent' header from the current request. Write the code to accomplish this and store the value in a variable called $userAgent.
//===================================
Route::any( '/example', function ( Request $request ) {
// Retrieve the value of the 'User-Agent' header from the request
$userAgent = $request->header( 'User-Agent' );
} );
//Question 3:
//You are building an API endpoint in Laravel that accepts a GET request with a 'page' query parameter. Write the code to retrieve the value of the 'page' parameter from the current request and store it in a variable called $page. If the parameter is not present, set $page to null.
//===============================
Route::get('/api/endpoint', function (Request $request) {
// Retrieve the value of the 'page' query parameter from the request
$page = $request->query('page');
// Set $page to null if the parameter is not present
if (!$page) {
$page = null;
}
});
//Question 4:
//Create a JSON response in Laravel with the following data.
Route::get( '/api/endpoint', function () {
$data = [
'name' => 'John Doe',
'age' => 35
];
$responseData = [
'message' => 'Success',
'data' => $data
];
return response()->json(
$responseData
);
} );
//Question 5:
//You are implementing a file upload feature in your Laravel application. Write the code to handle a file upload named 'avatar' in the current request and store the uploaded file in the 'public/uploads' directory. Use the original filename for the uploaded file.
//===============================================
Route::post( '/upload', function ( Request $request ) {
if ( $request->hasFile( 'avatar' ) ) {
$file = $request->file( 'avatar' );
$path = $file->store( 'uploads', 'public' );
return response()->json(
[
'message' => 'File uploaded successfully'
]
);
}
return response()->json(
[
'message' => 'No file was uploaded'
],
400 );
} );
//Question 6:
//Retrieve the value of the 'remember_token' cookie from the current request in Laravel and store it in a variable called $rememberToken. If the cookie is not present, set $rememberToken to null.
//======================================
Route::any( '/example', function ( Request $request ) {
// Retrieve the value of the 'remember_token' cookie from the request
$rememberToken = $request->cookie( 'remember_token' );
// Set $rememberToken to null if the cookie is not present
if ( is_null( $rememberToken ) ) {
$rememberToken = null;
}
} );
//Question 7:
//Create a route in Laravel that handles a POST request to the '/submit' URL. Inside the route closure, retrieve the 'email' input parameter from the request and store it in a variable called $email. Return a JSON response with the following data
//===============================================
Route::post( '/submit', function ( Request $request ) {
// Retrieve the 'email' input parameter from the request
$email = $request->input( 'email' );
// Create the JSON response data
$responseData = [
'success' => true,
'message' => 'Form submitted successfully.'
];
// Return a JSON response
return response()->json(
$responseData
);
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment