Table of Contents
Ever accidentally sent test data to a production API or uploaded development files to a live server? Here’s a simple guard rail: wrap critical operations in production environment checks.
The Problem
When you’re building workflows that interact with external systems, it’s easy to forget you’re running in development:
public function generateReport()
{
$csv = $this->generateCsv();
$encrypted = $this->encrypt($csv);
$this->uploadToSftp($encrypted); // ⚠️ Always uploads!
$this->sendNotification();
}
Run this in staging to test CSV generation? Congratulations, you just uploaded test data to the client’s production SFTP server.
The Fix
Use App::environment('production') to gate critical operations:
use Illuminate\Support\Facades\App;
public function generateReport()
{
$csv = $this->generateCsv();
$encrypted = $this->encrypt($csv);
if (App::environment('production')) {
$this->uploadToSftp($encrypted);
$this->output->writeln('✅ Uploaded to production SFTP');
} else {
$this->output->writeln('⏭️ Skipped SFTP upload (non-production)');
}
$this->sendNotification();
}
Now you can test the entire workflow in development—generate files, validate data, encrypt payloads—without triggering the actual external call.
When to Use This
Gate anything with external side effects:
- SFTP/FTP uploads
- External API calls (payments, third-party services)
- Email sends to real addresses
- Webhook deliveries
Keep the rest of your logic environment-agnostic. This way you catch bugs in staging without impacting production systems.
Pro Tip
Add console output when you skip operations. Future-you (debugging why files aren’t uploading in staging) will thank present-you.
Leave a Reply