Changeset View
Changeset View
Standalone View
Standalone View
src/app/Providers/RouteServiceProvider.php
<?php | <?php | ||||
namespace App\Providers; | namespace App\Providers; | ||||
use Illuminate\Support\Facades\Route; | use Illuminate\Cache\RateLimiting\Limit; | ||||
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider; | use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider; | ||||
use Illuminate\Http\Request; | |||||
use Illuminate\Support\Facades\RateLimiter; | |||||
use Illuminate\Support\Facades\Route; | |||||
class RouteServiceProvider extends ServiceProvider | class RouteServiceProvider extends ServiceProvider | ||||
{ | { | ||||
/** | /** | ||||
* This namespace is applied to your controller routes. | |||||
* | |||||
* In addition, it is set as the URL generator's root namespace. | |||||
* | |||||
* @var string | |||||
*/ | |||||
protected $namespace = 'App\Http\Controllers'; | |||||
/** | |||||
* Define your route model bindings, pattern filters, etc. | * Define your route model bindings, pattern filters, etc. | ||||
* | * | ||||
* @return void | * @return void | ||||
*/ | */ | ||||
public function boot() | public function boot() | ||||
{ | { | ||||
// | $this->configureRateLimiting(); | ||||
parent::boot(); | $this->routes(function () { | ||||
} | $prefix = \trim(\parse_url(\config('app.url'), PHP_URL_PATH), '/') . '/'; | ||||
/** | Route::prefix($prefix . 'api') | ||||
* Define the routes for the application. | ->group(base_path('routes/api.php')); | ||||
* | |||||
* @return void | |||||
*/ | |||||
public function map() | |||||
{ | |||||
$this->mapApiRoutes(); | |||||
$this->mapWebRoutes(); | |||||
// | |||||
} | |||||
/** | |||||
* Define the "web" routes for the application. | |||||
* | |||||
* These routes all receive session state, CSRF protection, etc. | |||||
* | |||||
* @return void | |||||
*/ | |||||
protected function mapWebRoutes() | |||||
{ | |||||
Route::middleware('web') | Route::middleware('web') | ||||
->namespace($this->namespace) | |||||
->group(base_path('routes/web.php')); | ->group(base_path('routes/web.php')); | ||||
}); | |||||
} | } | ||||
/** | /** | ||||
* Define the "api" routes for the application. | * Configure the rate limiters for the application. | ||||
* | |||||
* These routes are typically stateless. | |||||
* | * | ||||
* @return void | * @return void | ||||
*/ | */ | ||||
protected function mapApiRoutes() | protected function configureRateLimiting() | ||||
{ | { | ||||
// Note: We removed the prefix from here, to have more control | RateLimiter::for('api', function (Request $request) { | ||||
// over it in routes/api.php | return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip()); | ||||
Route::middleware('api') | }); | ||||
->namespace($this->namespace) | |||||
->group(base_path('routes/api.php')); | |||||
} | } | ||||
} | } |