我的端点为什么返回Laravel首页而不是响应?

huangapple go评论80阅读模式
英文:

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-Typeapplication/json

但是,我收到的不是错误消息或成功创建用户的响应,而是Laravel的首页(这是在首次运行Laravel项目时显示的页面)。

英文:

I am trying to do an API with laravel and this is my UserController which is inside App\Http\Controllers

&lt;?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-&gt;validate([
            &#39;fullname&#39; =&gt; &#39;required|string&#39;,
            &#39;gender&#39; =&gt; &#39;required|in:male,female,other&#39;,
            &#39;birth_date&#39; =&gt; &#39;required|date&#39;,
            &#39;email&#39; =&gt; &#39;required|string|email|unique:users&#39;,
            &#39;password&#39; =&gt; &#39;required|string&#39;,
        ]);
    
        $user = new User([
            &#39;fullname&#39; =&gt; $request-&gt;fullname,
            &#39;gender&#39; =&gt; $request-&gt;gender,
            &#39;birth_date&#39; =&gt; $request-&gt;birth_date,
            &#39;email&#39; =&gt; $request-&gt;email,
            &#39;password&#39; =&gt; $request-&gt;password
        ]);
    
        $user-&gt;save();
    
        return response()-&gt;json([&#39;message&#39; =&gt; &#39;User created successfully&#39;], 201);
    }
}

This is my api.php:

&lt;?php

use App\Http\Controllers\UserController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;


Route::middleware(&#39;auth:sanctum&#39;)-&gt;get(&#39;/user&#39;, function (Request $request) {
    return $request-&gt;user();
});

Route::post(&#39;/register&#39;, [UserController::class, &#39;create&#39;]);

In postman I am sending the following request to http://localhost:8000/api/register

{
    &quot;fullname&quot;: &quot;test&quot;,
    &quot;gender&quot;: &quot;Male&quot;,
    &quot;birth_date&quot;: &quot;10-02-1998&quot;,
    &quot;email&quot;: &quot;test@gmail.com&quot;,
    &quot;password&quot;: &quot;123456&quot;
}

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

 &quot;Content-Type&quot;: &quot;application/json&quot;
 &quot;Accept&quot;: &quot;application/json&quot;

huangapple
  • 本文由 发表于 2023年6月22日 06:16:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76527483.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定