API路由Laravel 5.5

时间:2018-10-29 20:19:44

标签: php api laravel-5

我有基本的控制器,我希望它在其中接收任何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);

  }
}

1 个答案:

答案 0 :(得分:2)

在app / Providers / RouteServiceProvider.php中。

删除前缀('api'),看起来像这样。

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