【问题标题】:API Routing Laravel 5.5API 路由 Laravel 5.5
【发布时间】:2019-04-02 20:10:45
【问题描述】:

我有基本的控制器,我希望它接收任何 json 请求。我是 api 路由的新手。当我使用 POST MAN 时,我很抱歉没有找到页面。首先,我在 GET 上对其进行了测试,并使其调用了简单的返回,但抛出了错误。“抱歉,找不到您要查找的页面。” 我删除了 RouteServiceProvider.php 中的 api 前缀,但没有成功。我放了我的演示控制器

路由api.php

<?php

use Illuminate\Http\Request;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/


Route::get('/test_api/v1', 'TestController@formCheck');

TestController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class TestController extends Controller
{
  public function formCheck(){
    return "YES !!!";
  }

  public function formPost(Request $request)
  {
    $formData = $request->all();
    return response()->json($formData, 201);

  }
}

【问题讨论】:

  • 你忘了这个吗:use App\Http\Controllers\Controller; ?
  • 您尝试点击哪个网址? api.php 下的所有文件都有 /api/ 前缀,因此您需要点击 www.app.com/api/test_api/v1
  • @Phiter 我正在尝试点击 /test_api/v1 只是尝试我的第一个方法 formCheck,在我的邮递员 localhost:8000/test_api/v1 上
  • /test_api之前添加/api

标签: php api laravel-5


【解决方案1】:

在 app/Providers/RouteServiceProvider.php 中。

删除前缀('api')&它应该看起来像这样。

protected function mapApiRoutes()
    {
        Route::middleware('api')
             ->namespace($this->namespace)
             ->group(base_path('routes/api.php'));
    }

【讨论】:

    猜你喜欢
    • 2018-08-05
    • 2018-08-31
    • 2018-06-23
    • 2018-03-01
    • 1970-01-01
    • 2018-09-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-04
    相关资源
    最近更新 更多