为什么我的路由莫名其妙

我按照教程写了个路由routes\api.php

<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

Route::prefix('v1')->namespace('Api')->name('api.v1')->group(function() {

    // 短信验证码
    Route::post('verificationCodes', 'VerificationCodesController@store')
        ->name('verificationCodes.store');
});

用户访问http://xxxx.com/api/v1/verificationCodes然后就开始执行下面的程序,没错吧

<?php

namespace App\Http\Controllers\Api;

use Illuminate\Support\Str;
use Illuminate\Http\Request;
use Overtrue\EasySms\EasySms;
use App\Http\Requests\Api\VerificationCodeRequest;

class VerificationCodesController extends Controller
{
    public function store(VerificationCodeRequest $request, EasySms $easySms)
    {
        $phone = $request->phone;
        .
        .
        .
    }
}

但是,这个时候,用postman看,就给我返回了一堆html,然后在chrome里看这些html就是laravel的默认首页,我路由没有写这些东西啊,也不报错,就很奇怪。

然后我检查了app\Http\Request\Api\ VerificationCodeRequest这个文件,里面也没写什么啊

<?php

namespace App\Http\Requests\Api;

class VerificationCodeRequest extends FormRequest
{
    public function rules()
    {
        return [
            'phone' => 'required|phone:CN,mobile|unique:users',
        ];
    }
}

为什么会返回首页的html呢

《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
最佳答案

Nginx配置加这个 location 了吗:

server {
    listen      80;
    server_name localhost;
    root        /var/www/joker/public;
    index       index.html index.htm index.php;
    # 这里的匹配规则
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
}

另外,在你的 postman 的 Header 中加入 Accept : application/json 就会返回 json 格式的数据了

4年前 评论
wongvio (楼主) 4年前
wongvio (楼主) 4年前
讨论数量: 3

Nginx配置加这个 location 了吗:

server {
    listen      80;
    server_name localhost;
    root        /var/www/joker/public;
    index       index.html index.htm index.php;
    # 这里的匹配规则
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
}

另外,在你的 postman 的 Header 中加入 Accept : application/json 就会返回 json 格式的数据了

4年前 评论
wongvio (楼主) 4年前
wongvio (楼主) 4年前

laravel的Request 验证失败会跳转首页 :joy:

4年前 评论

表单验证失败了锕

4年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!