Membangun aplikasi web di era modern memerlukan pondasi yang kokoh. Framework modern memberikan fleksibilitas luar biasa bagi developer untuk menghadirkan fitur kompleks secara cepat tanpa mengabaikan aspek keamanan dan keandalan sistem.
Tahapan Implementasi & Walkthrough Kode
Berikut adalah langkah demi langkah teknis yang dapat Anda ikuti langsung pada codebase Anda:
Tahap 1: Penulisan Kode & Konfigurasi
<?php
namespace App\Http\Controllers;
//import Model "Post
use App\Models\Post;
use Illuminate\Http\Request;
//return type View
use Illuminate\View\View;
//return type redirectResponse
use Illuminate\Http\RedirectResponse;
//import Facade "Storage"
use Illuminate\Support\Facades\Storage;
class PostController extends Controller
{
/**
* index
*
* @return View
*/
public function index(): View
{
//get posts
$posts = Post::latest()->paginate(5);
//render view with posts
return view('posts.index', compact('posts'));
}
/**
* create
*
* @return View
*/
public function create(): View
{
return view('posts.create');
}
/**
* store
*
* @param mixed $request
* @return RedirectResponse
*/
public function store(Request $request): RedirectResponse
{
//validate form
$this->validate($request, [
'image' => 'required|image|mimes:jpeg,jpg,png|max:2048',
'title' => 'required|min:5',
'content' => 'required|min:10'
]);
//upload image
$image = $request->file('image');
$image->storeAs('public/posts', $image->hashName());
//create post
Post::create([
'image' => $image->hashName(),
'title' => $request->title,
'content' => $request->content
]);
//redirect to index
return redirect()->route('posts.index')->with(['success' => 'Data Berhasil Disimpan!']);
}
/**
* show
*
* @param mixed $id
* @return View
*/
public function show(string $id): View
{
//get post by ID
$post = Post::findOrFail($id);
//render view with post
return view('posts.show', compact('post'));
}
/**
* edit
*
* @param mixed $id
* @return View
*/
public function edit(string $id): View
{
//get post by ID
$post = Post::findOrFail($id);
//render view with post
return view('posts.edit', compact('post'));
}
/**
* update
*
* @param mixed $request
* @param mixed $id
* @return RedirectResponse
*/
public function update(Request $request, $id): RedirectResponse
{
//validate form
$this->validate($request, [
'image' => 'image|mimes:jpeg,jpg,png|max:2048',
'title' => 'required|min:5',
'content' => 'required|min:10'
]);
//get post by ID
$post = Post::findOrFail($id);
//check if image is uploaded
if ($request->hasFile('image')) {
//upload new image
$image = $request->file('image');
$image->storeAs('public/posts', $image->hashName());
//delete old image
Storage::delete('public/posts/'.$post->image);
//update post with new image
$post->update([
'image' => $image->hashName(),
'title' => $request->title,
'content' => $request->content
]);
} else {
//update post without image
$post->update([
'title' => $request->title,
'content' => $request->content
]);
}
//redirect to index
return redirect()->route('posts.index')->with(['success' => 'Data Berhasil Diubah!']);
}
/**
* destroy
*
* @param mixed $post
* @return void
*/
public function destroy($id): RedirectResponse
{
//get post by ID
$post = Post::findOrFail($id);
//delete image
Storage::delete('public/posts/'. $post->image);
//delete post
$post->delete();
//redirect to index
return redirect()->route('posts.index')->with(['success' => 'Data Berhasil Dihapus!']);
}
}
Tahap 2: Penulisan Kode & Konfigurasi
public function destroy($id): RedirectResponse
{
//...
}
Tahap 3: Penulisan Kode & Konfigurasi
//get post by ID
$post = Post::findOrFail($id);
Tahap 4: Penulisan Kode & Konfigurasi
//delete image
Storage::delete('public/posts/'. $post->image);
Tahap 5: Penulisan Kode & Konfigurasi
//delete post
$post->delete();
Tahap 6: Penulisan Kode & Konfigurasi
//redirect to index
return redirect()->route('posts.index')->with(['success' => 'Data Berhasil Dihapus!']);
Selalu gunakan pendekatan deklaratif, validasi input seawal mungkin di boundary layer, dan lakukan logging terstruktur untuk memudahkan observability ketika sistem dijalankan di lingkungan cloud produksi.
Kesimpulan
Dengan mengikuti langkah-langkah terstruktur di atas, implementasi modul Anda akan jauh lebih rapi, aman dari bug umum, serta siap diskalakan ke lingkungan produksi.
Nantikan artikel tutorial praktis lainnya seputar fullstack development, microservices, dan otomasi AI di Blog Engineering SiHubung. Selamat mencoba!