laravel:template-method-and-plugins
Template Method and Pluggable Strategies
Keep core flows stable; enable extension via small classes.
Template Method
Use a base class that defines the algorithm skeleton; subclasses override hooks.
abstract class Importer {
final public function handle(string $path): void {
$rows = $this->load($path);
$data = $this->normalize($rows);
$this->validate($data);
$this->save($data);
}
abstract protected function load(string $path): array;
protected function normalize(array $rows): array { return $rows; }
protected function validate(array $data): void {}
abstract protected function save(array $data): void;
}
Strategy/Plugin
Define an interface; register implementations; select by key/config.
interface PaymentGateway { public function charge(int $cents, string $currency): string; }
final class StripeGateway implements PaymentGateway { /* ... */ }
final class BraintreeGateway implements PaymentGateway { /* ... */ }
final class PaymentsRegistry {
/** @param array<string,PaymentGateway> $drivers */
public function __construct(private array $drivers) {}
public function for(string $key): PaymentGateway { return $this->drivers[$key]; }
}
Prefer adding a class over editing switch statements. Test implementations in isolation.
More from jpcaparas/superpowers-laravel
laravel:routes-best-practices
Keep routes clean and focused on mapping requests to controllers; avoid business logic, validation, or database operations in route files
88laravel:blade-components-and-layouts
Compose UIs with Blade components, slots, and layouts; keep templates pure and testable
88laravel:quality-checks
Unified quality gates for Laravel projects; Pint, static analysis (PHPStan/Psalm), Insights (optional), and JS linters; Sail and non-Sail pairs provided
79laravel:performance-caching
Use framework caches and value/query caching to reduce work; add tags, locks, and explicit invalidation strategies for correctness
76laravel:eloquent-relationships
Define clear relationships and load data efficiently; prevent N+1, use constraints, counts/sums, and pivot syncing safely
75laravel:queues-and-horizon
Operate and verify queues with or without Horizon; safe worker flags, failure handling, and test strategies
74