coste/now-bundle package provides a lightweight, declarative way to inject the current timestamp into controller actions, reducing boilerplate (e.g., Carbon::now() calls). This aligns well with:
Carbon: More flexible but requires manual instantiation.$this->app->bind()) or config-based setup. Example:
$this->app->bind(CurrentDate::class, function () {
return new CurrentDate(now());
});
public function store(CurrentDate $now) {
// $now->format('Y-m-d') ...
}
CurrentDate easily (e.g., using Mockery or Laravel’s partialMock).config(app.timezone). Ensure this matches your application’s requirements (e.g., UTC for APIs).now() call per request). Benchmark if used in high-throughput endpoints.now() calls? (e.g., "To enforce consistency across 50+ controllers.")CurrentDate in unit/integration tests? (e.g., now()->freeze() for deterministic tests.)CurrentUser, CurrentIP)?TimestampableController) reduce boilerplate further?$request->server('REQUEST_TIME')) or facades (Carbon::now()) suffice.CurrentDate to add Carbon methods if needed.now() calls. Identify high-value targets (e.g., store(), update()).public function create(Request $request) {
$createdAt = Carbon::now(); // Manual
// ...
}
With:
public function create(CurrentDate $now) {
$createdAt = $now; // Injected
// ...
}
AdminController). Test edge cases (e.g., timezone, performance).composer.json and run composer require coste/now-bundle.AppServiceProvider):
public function register() {
$this->app->bind(CurrentDate::class, fn() => new CurrentDate(now()));
}
aliases in config/app.php if using facades (though DI is preferred).now() calls, replacing with CurrentDate or @inject in Blade.Illuminate\Container\Container (e.g., PHP 8.1 constructor property promotion).app()->bind() vs. bind()).CurrentDate class name). Rename if needed:
$this->app->bind('app.CurrentDate', fn() => new CurrentDate(now()));
ext-date enabled.CurrentDate (start with critical paths).tideways/xhprof).Carbon::now() calls.DateTimeImmutable).config/app.php and CurrentDate usage.now() vs. config('app.timezone')).dd($now) to inspect injected timestamps.CurrentDate.## Timestamp Injection
Inject the current date into controllers:
```php
public function __construct(private CurrentDate $now) {}
now() call per request). No database or external calls.now() in long-running processes (e.g., queues) if precision is critical:
$now = now()->startOfSecond(); // Reduce microsecond noise
now() (unlikelyHow can I help you explore Laravel packages today?