Initial Laravel 12 starter kit
This commit is contained in:
62
app/Providers/AppServiceProvider.php
Normal file
62
app/Providers/AppServiceProvider.php
Normal file
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use Illuminate\Cache\RateLimiting\Limit;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\RateLimiter;
|
||||
use Illuminate\Support\Facades\URL;
|
||||
use Illuminate\Support\Facades\Vite;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class AppServiceProvider extends ServiceProvider {
|
||||
/**
|
||||
* Register any application services.
|
||||
*/
|
||||
public function register(): void {
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Bootstrap any application services.
|
||||
*/
|
||||
public function boot(): void {
|
||||
// Prevents lazy loading
|
||||
// Prevents silently discarding attributes.
|
||||
// Prevents accessing missing attributes.
|
||||
Model::shouldBeStrict();
|
||||
|
||||
// Automatically eager-load needed relations.
|
||||
Model::automaticallyEagerLoadRelationships();
|
||||
|
||||
// Force HTTPS for all generated URLs.
|
||||
URL::forceHttps(
|
||||
$this->app->environment(['production']),
|
||||
);
|
||||
|
||||
// Prohibits: db:wipe, migrate:fresh, migrate:refresh, and migrate:reset
|
||||
DB::prohibitDestructiveCommands($this->app->environment(['production']));
|
||||
|
||||
// Prefetch all assets at once.
|
||||
Vite::prefetch();
|
||||
|
||||
// Disable resourc wrapping
|
||||
JsonResource::withoutWrapping();
|
||||
|
||||
// Globally rate limit api requests
|
||||
RateLimiter::for('api', function (Request $request) {
|
||||
return Limit::perMinute(500)
|
||||
->by($request->user()->id ?? $request->ip())
|
||||
->response(function () {
|
||||
return response()->json([
|
||||
'message' => 'Too many attempts. Try again later.',
|
||||
], 429);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user