Pendahuluan
Laravel telah menjadi salah satu framework PHP paling populer untuk pengembangan web modern. Menerapkan best practices tidak hanya membuat kode Anda lebih maintainable, tetapi juga meningkatkan performa dan keamanan aplikasi.
Mengapa Best Practices Penting?
- Meningkatkan maintainability kode
- Mempermudah kolaborasi tim
- Mengurangi technical debt
- Mengoptimalkan performa aplikasi
- Meningkatkan keamanan
Target Pembaca
- Developer Laravel tingkat menengah
- Senior developers
- Tech leads & software architects
- DevOps engineers
๐ Arsitektur & Struktur Proyek
Struktur Folder Optimal
โโ app
โ โโโ Console
โ โโโ Contracts
โ โโโ Events
โ โโโ Exceptions
โ โโโ Http
โ โ โโโ Controllers
โ โ โโโ Middleware
โ โ โโโ Requests
โ โโโ Jobs
โ โโโ Listeners
โ โโโ Models
โ โโโ Providers
โ โโโ Repositories
โ โโโ Services
Konvensi Penamaan
Classes & Files
// Good
class UserController extends Controller
class CreateUserRequest
class UserRepository
// Bad
class users_controller
class create_user
Implementasi Design Patterns
Repository Pattern
interface UserRepositoryInterface
{
public function find($id);
public function create(array $data);
}
class UserRepository implements UserRepositoryInterface
{
public function find($id)
{
return User::find($id);
}
public function create(array $data)
{
return User::create($data);
}
}
๐พ Database & Model Best Practices
Migrations
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
$table->softDeletes();
});
}
Eloquent Relationships
class User extends Model
{
public function posts()
{
return $this->hasMany(Post::class);
}
public function profile()
{
return $this->hasOne(Profile::class);
}
}
๐ฎ Controller & Route Best Practices
RESTful Controllers
class UserController extends Controller
{
public function index()
{
return User::paginate(20);
}
public function store(CreateUserRequest $request)
{
return User::create($request->validated());
}
}
Route Organization
Route::group(['middleware' => 'auth:api'], function () {
Route::apiResource('users', UserController::class);
Route::apiResource('posts', PostController::class);
});
๐ Security Best Practices
Authentication
// config/auth.php
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'sanctum',
'provider' => 'users',
],
],
Request Validation
class CreateUserRequest extends FormRequest
{
public function rules()
{
return [
'name' => 'required|string|max:255',
'email' => 'required|email|unique:users',
'password' => 'required|min:8|confirmed',
];
}
}
โก Performance Optimization
Caching Strategies
// Cache query results
$users = Cache::remember('users', 3600, function () {
return User::active()->get();
});
// Cache views
@cache('key', 60)
@include('expensive.view')
@endcache
Query Optimization
// Good
$users = User::with('posts')->get();
// Bad N+1 Problem
$users = User::all();
foreach ($users as $user) {
$user->posts;
}
๐งช Testing Best Practices
Unit Testing
public function test_user_can_be_created()
{
$userData = [
'name' => 'John Doe',
'email' => '[email protected]',
'password' => 'password'
];
$user = User::create($userData);
$this->assertDatabaseHas('users', [
'email' => '[email protected]'
]);
}
๐ Deployment Checklist
Pre-deployment
Optimize autoloader: `composer install --optimize-autoloader --no-dev`
Cache configuration: `php artisan config:cache`
Cache routes: `php artisan route:cache`
Cache views: `php artisan view:cache`
Set appropriate permissions
Update environment variables
๐ Resources & Further Reading
- Laravel Official Documentation
- Laravel Best Practices
- Laravel News
๐ค Community & Support
Kamu bisa bergabung dengan komunitas Laravel Indonesia untuk diskusi lebih: