]> BookStack Code Mirror - bookstack/blob - app/App/Providers/RouteServiceProvider.php
3a155920eb4f681ee313a291a8741e43169d7d81
[bookstack] / app / App / Providers / RouteServiceProvider.php
1 <?php
2
3 namespace BookStack\App\Providers;
4
5 use BookStack\Facades\Theme;
6 use BookStack\Theming\ThemeEvents;
7 use Illuminate\Cache\RateLimiting\Limit;
8 use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
9 use Illuminate\Http\Request;
10 use Illuminate\Routing\Router;
11 use Illuminate\Support\Facades\RateLimiter;
12 use Illuminate\Support\Facades\Route;
13
14 class RouteServiceProvider extends ServiceProvider
15 {
16     /**
17      * The path to the "home" route for your application.
18      *
19      * This is used by Laravel authentication to redirect users after login.
20      *
21      * @var string
22      */
23     public const HOME = '/';
24
25     /**
26      * Define your route model bindings, pattern filters, etc.
27      */
28     public function boot(): void
29     {
30         $this->configureRateLimiting();
31
32         $this->routes(function () {
33             $this->mapWebRoutes();
34             $this->mapApiRoutes();
35         });
36     }
37
38     /**
39      * Define the "web" routes for the application.
40      *
41      * These routes all receive session state, CSRF protection, etc.
42      */
43     protected function mapWebRoutes(): void
44     {
45         Route::group([
46             'middleware' => 'web',
47             'namespace'  => $this->namespace,
48         ], function (Router $router) {
49             require base_path('routes/web.php');
50             Theme::dispatch(ThemeEvents::ROUTES_REGISTER_WEB, $router);
51         });
52
53         Route::group([
54             'middleware' => ['web', 'auth'],
55         ], function (Router $router) {
56             Theme::dispatch(ThemeEvents::ROUTES_REGISTER_WEB_AUTH, $router);
57         });
58     }
59
60     /**
61      * Define the "api" routes for the application.
62      *
63      * These routes are typically stateless.
64      */
65     protected function mapApiRoutes(): void
66     {
67         Route::group([
68             'middleware' => 'api',
69             'namespace'  => $this->namespace . '\Api',
70             'prefix'     => 'api',
71         ], function ($router) {
72             require base_path('routes/api.php');
73         });
74     }
75
76     /**
77      * Configure the rate limiters for the application.
78      */
79     protected function configureRateLimiting(): void
80     {
81         RateLimiter::for('api', function (Request $request) {
82             return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip());
83         });
84     }
85 }