1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Providers; |
4
|
|
|
|
5
|
|
|
use App\Enum\OrderStatus; |
6
|
|
|
use Illuminate\Cache\RateLimiting\Limit; |
7
|
|
|
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider; |
8
|
|
|
use Illuminate\Http\Request; |
9
|
|
|
use Illuminate\Support\Facades\RateLimiter; |
10
|
|
|
use Illuminate\Support\Facades\Route; |
11
|
|
|
|
12
|
|
|
class RouteServiceProvider extends ServiceProvider |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* The path to the "home" route for your application. |
16
|
|
|
* |
17
|
|
|
* This is used by Laravel authentication to redirect users after login. |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
public const HOME = '/'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* {@inheritDoc} |
25
|
|
|
*/ |
26
|
|
|
// protected $namespace = 'App\\Http\\Controllers'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* List of request parameter that can be resolved into an enum instance. |
30
|
|
|
* |
31
|
|
|
* @var string[] |
32
|
|
|
*/ |
33
|
|
|
protected $enums = [ |
34
|
|
|
'enumOrderStatus' => OrderStatus::class, |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Define your route model bindings, pattern filters, etc. |
39
|
|
|
* |
40
|
|
|
* @return void |
41
|
|
|
*/ |
42
|
73 |
|
public function boot() |
43
|
|
|
{ |
44
|
73 |
|
$this->configureRateLimiting(); |
45
|
|
|
|
46
|
73 |
|
$this->routes(function () { |
47
|
73 |
|
Route::prefix('api') |
48
|
73 |
|
->middleware('api') |
49
|
73 |
|
->namespace($this->namespace) |
50
|
73 |
|
->group(base_path('routes/api.php')); |
51
|
|
|
|
52
|
73 |
|
Route::middleware('web') |
53
|
73 |
|
->namespace($this->namespace) |
54
|
73 |
|
->group(base_path('routes/web.php')); |
55
|
|
|
|
56
|
73 |
|
Route::match(['get', 'post'], '/botman', function () { |
57
|
|
|
$this->mapBotManCommands(); |
58
|
73 |
|
})->middleware('web_without_csrf'); |
59
|
73 |
|
}); |
60
|
|
|
|
61
|
73 |
|
$this->resolveEnumBinding(); |
62
|
73 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Configure the rate limiters for the application. |
66
|
|
|
* |
67
|
|
|
* @return void |
68
|
|
|
*/ |
69
|
73 |
|
protected function configureRateLimiting() |
70
|
|
|
{ |
71
|
73 |
|
RateLimiter::for('api', function (Request $request) { |
72
|
|
|
return Limit::perMinute(60)->by(optional($request->user())->id ?: $request->ip()); |
73
|
73 |
|
}); |
74
|
73 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Defines the BotMan "hears" commands. |
78
|
|
|
* |
79
|
|
|
* Note: Please don't remove this below file, as it will be used also on the artisan command `botman:tinker` |
80
|
|
|
* |
81
|
|
|
* @return void |
82
|
|
|
*/ |
83
|
|
|
protected function mapBotManCommands() |
84
|
|
|
{ |
85
|
|
|
require base_path('routes/botman.php'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Resolve enum binding from route parameter. |
90
|
|
|
* |
91
|
|
|
* @return void |
92
|
|
|
*/ |
93
|
73 |
|
protected function resolveEnumBinding() |
94
|
|
|
{ |
95
|
73 |
|
foreach ($this->enums as $name => $class) { |
96
|
73 |
|
Route::bind($name, function ($value) use ($class) { |
97
|
|
|
return $class::from($value); |
98
|
73 |
|
}); |
99
|
|
|
} |
100
|
73 |
|
} |
101
|
|
|
} |
102
|
|
|
|