Pernah nggak ngalamin situasi kayak gini
project Laravel baru diclone dari repository, terus ada developer baru yang join.
Mereka harus setup database, bikin user admin, configure storage, dan sederetan langkah setup lainnya. Ujung-ujungnya? Bingung dan sering miss step.
Nah, solusi yang elegan adalah bikin Laravel Command khusus untuk setup awal project. Dengan satu perintah sederhana, semua konfigurasi dan setup beres otomatis. Developer baru tinggal jalanin command, duduk manis, dan project siap dipakai.
Kenapa Setup Command Itu Penting?
Bayangin kamu punya tim yang terus berkembang. Setiap ada developer baru, kamu harus jelasin step-by-step cara setup project. Atau lebih parah lagi, mereka ikutin dokumentasi yang outdated dan malah error. Ribet kan?
Dengan setup command, kamu bisa:
- Standarisasi proses setup di semua environment
- Hemat waktu onboarding developer baru
- Kurangi human error saat setup
- Dokumentasi yang "executable" - langsung jalan, bukan cuma bacaan
Membuat Setup Command
Yuk kita bikin command setup yang comprehensive. Pertama, generate command-nya:
php artisan make:command SetupProject
Sekarang kita isi dengan logic setup yang lengkap:
<?php
namespace App\Console\Commands;
use App\Models\User;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Hash;
class SetupProject extends Command
{
protected $signature = 'project:setup';
protected $description = 'Setup project untuk development environment';
public function handle()
{
$this->info('🚀 Memulai setup project...');
$this->newLine();
// Step 1: Check .env file
if (!file_exists('.env')) {
$this->error('.env file tidak ditemukan!');
if ($this->confirm('Copy dari .env.example?', true)) {
copy('.env.example', '.env');
$this->info('✓ .env file berhasil dibuat');
} else {
return Command::FAILURE;
}
}
// Step 2: Generate app key
if (empty(config('app.key'))) {
$this->info('Generating application key...');
Artisan::call('key:generate');
$this->info('✓ Application key generated');
}
// Step 3: Database setup
$this->newLine();
$this->warn('📊 Database Setup');
if ($this->confirm('Jalankan migration?', true)) {
Artisan::call('migrate:fresh');
$this->info('✓ Database migration selesai');
}
// Step 4: Create admin user
$this->newLine();
$this->warn('👤 Admin User Setup');
if ($this->confirm('Buat akun admin?', true)) {
$this->createAdminUser();
}
// Step 5: Storage link
$this->newLine();
$this->info('Creating storage link...');
Artisan::call('storage:link');
$this->info('✓ Storage link created');
// Step 6: Cache clear
$this->newLine();
$this->info('Clearing cache...');
Artisan::call('cache:clear');
Artisan::call('config:clear');
Artisan::call('view:clear');
$this->info('✓ Cache cleared');
// Success message
$this->newLine();
$this->info('🎉 Setup project selesai!');
$this->info('Aplikasi siap digunakan di: ' . config('app.url'));
return Command::SUCCESS;
}
private function createAdminUser()
{
$name = $this->ask('Nama admin', 'Admin');
$email = $this->ask('Email admin', '[email protected]');
$password = $this->secret('Password admin (min 8 karakter)');
try {
User::create([
'name' => $name,
'email' => $email,
'password' => Hash::make($password),
'role' => 'admin'
]);
$this->info('✓ Admin user berhasil dibuat');
$this->table(
['Name', 'Email'],
[[$name, $email]]
);
} catch (\Exception $e) {
$this->error('Gagal membuat admin user: ' . $e->getMessage());
}
}
}
Cara Menggunakan Setup Command
Setelah command dibuat, developer baru tinggal jalankan satu perintah:
php artisan project:setup
Command akan interaktif menanyakan beberapa hal dan otomatis setup semua yang dibutuhkan. Gampang kan?
Tambahan: Setup Command dengan Mode
Kita bisa bikin lebih fleksibel dengan menambahkan mode development atau production:
protected $signature = 'project:setup {--mode=dev : Setup mode (dev/prod)}';
public function handle()
{
$mode = $this->option('mode');
$this->info("🚀 Setup project dalam mode: {$mode}");
if ($mode === 'dev') {
// Jalankan seeder untuk dummy data
if ($this->confirm('Seed dummy data?', true)) {
Artisan::call('db:seed');
$this->info('✓ Dummy data berhasil di-seed');
}
}
// setup lainnya...
}
Cara panggilnya:
# Development mode (dengan dummy data)
php artisan project:setup --mode=dev
# Production mode
php artisan project:setup --mode=prod
Command untuk Reset Project
Kadang kita juga butuh reset project ke kondisi awal. Bikin command khusus untuk ini:
php artisan make:command ResetProject
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;
class ResetProject extends Command
{
protected $signature = 'project:reset';
protected $description = 'Reset project ke kondisi awal';
public function handle()
{
if (!$this->confirm('⚠️ Ini akan menghapus semua data. Lanjutkan?')) {
return Command::FAILURE;
}
$this->warn('Mereset project...');
// Drop semua table dan migrate ulang
Artisan::call('migrate:fresh --seed');
// Clear semua cache
Artisan::call('cache:clear');
Artisan::call('config:clear');
Artisan::call('route:clear');
Artisan::call('view:clear');
$this->info('✓ Project berhasil direset');
$this->info('Jalankan: php artisan project:setup untuk setup ulang');
return Command::SUCCESS;
}
}
Update Documentation
Jangan lupa update README.md project kamu dengan instruksi yang simple:
## Setup Project
1. Clone repository
```bash
git clone [repository-url]
cd project-name
```
2. Install dependencies
```bash
composer install
npm install
```
3. Setup project
```bash
php artisan project:setup
```
4. Run development server
```bash
php artisan serve
```
Selesai! Project siap digunakan.
Tips Tambahan
Validasi Environment
Tambahkan pengecekan apakah environment sudah sesuai:
private function checkRequirements()
{
$requirements = [
'PHP' => version_compare(PHP_VERSION, '8.1.0', '>='),
'MySQL' => extension_loaded('pdo_mysql'),
'GD' => extension_loaded('gd'),
];
foreach ($requirements as $name => $passed) {
if ($passed) {
$this->info("✓ {$name} OK");
} else {
$this->error("✗ {$name} Not OK");
}
}
}
Seed Data Spesifik
Untuk mode development, bisa tambahkan seeder khusus:
if ($mode === 'dev') {
Artisan::call('db:seed', ['--class' => 'DevelopmentSeeder']);
}
Kesimpulan
Setup command adalah investasi kecil yang memberikan dampak besar untuk produktivitas tim. Developer baru bisa langsung productive tanpa harus bingung setup ini-itu. Dokumentasi jadi lebih simple, dan yang paling penting: setup process jadi konsisten di semua environment.
Jadi, daripada bikin dokumentasi setup yang panjang dan sering outdated, mending bikin setup command yang reliable dan mudah dijalankan. Trust me, tim kamu akan berterima kasih! 😊
...