質問編集履歴

1

問題点の変更

2024/04/18 13:18

投稿

junichiKato072
junichiKato072

スコア1

test CHANGED
File without changes
test CHANGED
@@ -10,10 +10,7 @@
10
10
  ### 発生している問題・エラーメッセージ
11
11
 
12
12
  ```
13
- auth.js:33
14
-
15
-
16
- POST http://localhost/api/auth/register net::ERR_EMPTY_RESPONSE
13
+ The GET method is not supported for route api/auth/register. Supported methods: POST, OPTIONS.
17
14
  ```
18
15
 
19
16
  ### 該当のソースコード
@@ -28,6 +25,125 @@
28
25
 
29
26
  Route::post('/auth/register', [\App\Http\Controllers\AuthController::class, 'register']);
30
27
  ```
28
+
29
+ CorsMiddleware.php
30
+ ```PHP
31
+ <?php
32
+
33
+ namespace App\Http\Middleware;
34
+
35
+ use Closure;
36
+
37
+ class CorsMiddleware
38
+ {
39
+ public function handle($request, Closure $next)
40
+ {
41
+ return $next($request)
42
+ ->header('Access-Control-Allow-Origin', '*')
43
+ ->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS')
44
+ ->header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
45
+ }
46
+ }
47
+ ```
48
+
49
+ Kernel.php
50
+ ```PHP
51
+ <?php
52
+
53
+ namespace App\Http;
54
+
55
+ use Illuminate\Foundation\Http\Kernel as HttpKernel;
56
+
57
+ class Kernel extends HttpKernel
58
+ {
59
+ /**
60
+ * The application's global HTTP middleware stack.
61
+ *
62
+ * These middleware are run during every request to your application.
63
+ *
64
+ * @var array<int, class-string|string>
65
+ */
66
+ protected $middleware = [
67
+ // \App\Http\Middleware\TrustHosts::class,
68
+ \App\Http\Middleware\TrustProxies::class,
69
+ \Illuminate\Http\Middleware\HandleCors::class,
70
+ \App\Http\Middleware\PreventRequestsDuringMaintenance::class,
71
+ \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
72
+ \App\Http\Middleware\TrimStrings::class,
73
+ \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
74
+ \App\Http\Middleware\CorsMiddleware::class,
75
+ ];
76
+
77
+ /**
78
+ * The application's route middleware groups.
79
+ *
80
+ * @var array<string, array<int, class-string|string>>
81
+ */
82
+ protected $middlewareGroups = [
83
+ 'web' => [
84
+ \App\Http\Middleware\EncryptCookies::class,
85
+ \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
86
+ \Illuminate\Session\Middleware\StartSession::class,
87
+ \Illuminate\View\Middleware\ShareErrorsFromSession::class,
88
+ \App\Http\Middleware\VerifyCsrfToken::class,
89
+ \Illuminate\Routing\Middleware\SubstituteBindings::class,
90
+ ],
91
+
92
+ 'api' => [
93
+ \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
94
+ 'throttle:api',
95
+ \Illuminate\Routing\Middleware\SubstituteBindings::class,
96
+ \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
97
+ ],
98
+ ];
99
+
100
+ /**
101
+ * The application's route middleware.
102
+ *
103
+ * These middleware may be assigned to groups or used individually.
104
+ *
105
+ * @var array<string, class-string|string>
106
+ */
107
+ protected $routeMiddleware = [
108
+ 'auth' => \App\Http\Middleware\Authenticate::class,
109
+ 'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
110
+ 'auth.session' => \Illuminate\Session\Middleware\AuthenticateSession::class,
111
+ 'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
112
+ 'can' => \Illuminate\Auth\Middleware\Authorize::class,
113
+ 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
114
+ 'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
115
+ 'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
116
+ 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
117
+ 'verified' => \App\Http\Middleware\EnsureEmailIsVerified::class,
118
+ ];
119
+ }
120
+ ```
121
+
122
+ AuthController.php
123
+ ```PHP
124
+ <?php
125
+
126
+ namespace App\Http\Controllers;
127
+
128
+ use Illuminate\Http\Request;
129
+ use App\Models\User;
130
+ use Illuminate\Support\Facades\Hash;
131
+
132
+ class AuthController extends Controller
133
+ {
134
+ public function register(Request $request)
135
+ {
136
+ $user = new User();
137
+ $user->name = $request->input('username');
138
+ $user->email = $request->input('email');
139
+ $user->password = Hash::make($request->input('password'));
140
+ $user->save();
141
+
142
+ return response()->json(['message' => 'User registered successfully'], 201);
143
+ }
144
+ }
145
+ ```
146
+
31
147
 
32
148
  auth.js
33
149
  ```JavaScript
@@ -80,6 +196,4 @@
80
196
  Vue.js 3.3.4
81
197
 
82
198
 
83
-
84
- ソースの詳細が必要であれば追って記載いたします。
85
199
  ご回答いただけると幸いです。