英文:
Why my endpoint is returning the Laravel homepage instead of the response?
问题
以下是您的代码的翻译部分:
我正在尝试使用Laravel创建一个API,这是我的`UserController`,位于`App\Http\Controllers`目录下:
```php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
class UserController extends Controller
{
public function index(){
$users = User::all();
return $users;
}
public function create(Request $request){
$request->validate([
'fullname' => 'required|string',
'gender' => 'required|in:male,female,other',
'birth_date' => 'required|date',
'email' => 'required|string|email|unique:users',
'password' => 'required|string',
]);
$user = new User([
'fullname' => $request->fullname,
'gender' => $request->gender,
'birth_date' => $request->birth_date,
'email' => $request->email,
'password' => $request->password
]);
$user->save();
return response()->json(['message' => 'User created successfully'], 201);
}
}
这是我的api.php
文件:
<?php
use App\Http\Controllers\UserController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
return $request->user();
});
Route::post('/register', [UserController::class, 'create']);
在Postman中,我发送了以下请求到http://localhost:8000/api/register
:
{
"fullname": "test",
"gender": "Male",
"birth_date": "10-02-1998",
"email": "test@gmail.com",
"password": "123456"
}
作为请求头,我使用了Content-Type
为application/json
。
但是,我收到的不是错误消息或成功创建用户的响应,而是Laravel的首页(这是在首次运行Laravel项目时显示的页面)。
英文:
I am trying to do an API with laravel and this is my UserController
which is inside App\Http\Controllers
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
class UserController extends Controller
{
public function index(){
$users = User::all();
return $users;
}
public function create(Request $request){
$request->validate([
'fullname' => 'required|string',
'gender' => 'required|in:male,female,other',
'birth_date' => 'required|date',
'email' => 'required|string|email|unique:users',
'password' => 'required|string',
]);
$user = new User([
'fullname' => $request->fullname,
'gender' => $request->gender,
'birth_date' => $request->birth_date,
'email' => $request->email,
'password' => $request->password
]);
$user->save();
return response()->json(['message' => 'User created successfully'], 201);
}
}
This is my api.php:
<?php
use App\Http\Controllers\UserController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
return $request->user();
});
Route::post('/register', [UserController::class, 'create']);
In postman I am sending the following request to http://localhost:8000/api/register
{
"fullname": "test",
"gender": "Male",
"birth_date": "10-02-1998",
"email": "test@gmail.com",
"password": "123456"
}
As headers i have Content-Type
application/json
Instead of getting an error or the response with the message that a user has been created, i get the Laravel home page (The one when you first run a laravel project).
答案1
得分: 1
你尝试使用以下标头:
"Content-Type": "application/json"
"Accept": "application/json"
英文:
you try use the headers
"Content-Type": "application/json"
"Accept": "application/json"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论