Backend Fundamental #Modul 2 - Intro To Integration
Backend Fundamental #Modul 2 - Intro To Integration
MODUL PRAKTIK
#2
Backend Fundamental: PHP & Restful API Laravel
Muhammad Raihan Nismara Jendra
1-26-2024
BAB I
PENGENALAN PHP
<!DOCTYPE html>
<html>
<body>
<?php
echo "My first PHP script!";
?>
</body> </html>
RESTful API menggunakan metode HTTP sebagai aturan dasar untuk berkomunikasi
antar layanan web. Setiap metode HTTP memiliki peran spesifik, sebagai contoh :
Setelah itu, buka terminal dan jalankan perintah berikut untuk mengeksekusi
file migration yang telah kita buat menjadi sebuah table di database.
namespace App\Models;
use HasFactory;
namespace App\Http\Controllers;
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
'title'=>'required',
'description'=>'required',
'image'=>'required|image'
]);
try{
$imageName = Str::random().'.'.$request->image-
>getClientOriginalExtension();
Storage::disk('public')->putFileAs('product/image',
$request->image,$imageName);
Product::create($request-
>post()+['image'=>$imageName]);
return response()->json([
'message'=>'Product Created Successfully!!'
]);
}catch(\Exception $e){
\Log::error($e->getMessage());
return response()->json([
'message'=>'Something goes wrong while creating
a product!!' ],500);
}
}
/**
* Display the specified resource.
*
* @param \App\Models\Product $product
* @return \Illuminate\Http\Response
*/
public function show(Product $product)
{
return response()->json([
'product'=>$product
]);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\Product $product
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Product $product)
{
$request->validate([
'title'=>'required',
'description'=>'required',
'image'=>'nullable'
]);
try{
$product->fill($request->post())->update();
if($request->hasFile('image')){
$imageName = Str::random().'.'.$request->image-
>getClientOriginalExtension();
Storage::disk('public')-
>putFileAs('product/image', $request->image,$imageName);
$product->image = $imageName;
$product->save();
}
return response()->json([
'message'=>'Product Updated Successfully!!'
]);
}catch(\Exception $e){
\Log::error($e->getMessage());
return response()->json([
'message'=>'Something goes wrong while updating
a product!!' ],500);
}
}
/**
* Remove the specified resource from storage.
*
* @param \App\Models\Product $product
* @return \Illuminate\Http\Response
*/
public function destroy(Product $product)
{
try {
if($product->image){
$exists =
Storage::disk('public')>exists("product/image/{$product-
>image}"); if($exists){
Storage::disk('public')-
>delete("product/image/{$product->image}");
}
}
$product->delete();
return response()->json([
'message'=>'Product Deleted Successfully!!'
]);
use App\Http\Controllers\ProductController;
Route::resource('products',ProductController::class);
Perintah diatas membuat pintu akses untuk file-file yang Anda simpan di folder
“storage/app/public”. Ini berguna ketika Anda ingin membagikan file-file tersebut ke
publik melalui web.