Laravel API问题与路线有关

时间:2018-06-18 11:03:51

标签: api laravel-5 postman

我在laravel中创建了一个api并按照这篇文章。最后得到一个错误。

https://medium.com/@dinotedesco/using-laravel-5-5-resources-to-create-your-own-json-api-formatted-api-2c6af5e4d0e8

文章管理员:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ArticleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
        return 'You calling'.$id;
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

ArticleResource.php

<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class ArticleResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        //return parent::toArray($request);
        return[
            'type' => 'articles',
            'id' => (string) $this->id,
            'attributes' => [
                'title' => $this->title;
            ]

        ]
    }
}

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::middleware('auth:api')->get('/user', function (Request $request) {
    return $request->user();
});


Route::resource('articles', 'ArticleController/show');

POSTMAN响应错误:

ReflectionException: Class App\Http\Controllers\ArticleController/show does not exist in file /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Container/Container.php on line 767
Stack trace:
  1. ReflectionException-&gt;() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Container/Container.php:767
  2. ReflectionClass-&gt;__construct() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Container/Container.php:767
  3. Illuminate\Container\Container-&gt;build() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Container/Container.php:646
  4. Illuminate\Container\Container-&gt;resolve() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Container/Container.php:601
  5. Illuminate\Container\Container-&gt;make() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:734
  6. Illuminate\Foundation\Application-&gt;make() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Routing/Route.php:226
  7. Illuminate\Routing\Route-&gt;getController() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Routing/Route.php:796
  8. Illuminate\Routing\Route-&gt;controllerMiddleware() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Routing/Route.php:757
  9. Illuminate\Routing\Route-&gt;gatherMiddleware() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Routing/Router.php:678
 10. Illuminate\Routing\Router-&gt;gatherRouteMiddleware() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Routing/Router.php:658
 11. Illuminate\Routing\Router-&gt;runRouteWithinStack() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Routing/Router.php:642
 12. Illuminate\Routing\Router-&gt;runRoute() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Routing/Router.php:608
 13. Illuminate\Routing\Router-&gt;dispatchToRoute() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Routing/Router.php:597
 14. Illuminate\Routing\Router-&gt;dispatch() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:176
 15. Illuminate\Foundation\Http\Kernel-&gt;Illuminate\Foundation\Http\{closure}() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:30
 16. Illuminate\Routing\Pipeline-&gt;Illuminate\Routing\{closure}() /opt/lampp/htdocs/apidemo/vendor/fideloper/proxy/src/TrustProxies.php:57
 17. Fideloper\Proxy\TrustProxies-&gt;handle() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:151
 18. Illuminate\Pipeline\Pipeline-&gt;Illuminate\Pipeline\{closure}() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
 19. Illuminate\Routing\Pipeline-&gt;Illuminate\Routing\{closure}() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php:31
 20. Illuminate\Foundation\Http\Middleware\TransformsRequest-&gt;handle() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:151
 21. Illuminate\Pipeline\Pipeline-&gt;Illuminate\Pipeline\{closure}() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
 22. Illuminate\Routing\Pipeline-&gt;Illuminate\Routing\{closure}() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php:31
 23. Illuminate\Foundation\Http\Middleware\TransformsRequest-&gt;handle() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:151
 24. Illuminate\Pipeline\Pipeline-&gt;Illuminate\Pipeline\{closure}() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
 25. Illuminate\Routing\Pipeline-&gt;Illuminate\Routing\{closure}() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php:27
 26. Illuminate\Foundation\Http\Middleware\ValidatePostSize-&gt;handle() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:151
 27. Illuminate\Pipeline\Pipeline-&gt;Illuminate\Pipeline\{closure}() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
 28. Illuminate\Routing\Pipeline-&gt;Illuminate\Routing\{closure}() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php:51
 29. Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode-&gt;handle() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:151
 30. Illuminate\Pipeline\Pipeline-&gt;Illuminate\Pipeline\{closure}() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
 31. Illuminate\Routing\Pipeline-&gt;Illuminate\Routing\{closure}() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:104
 32. Illuminate\Pipeline\Pipeline-&gt;then() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:151
 33. Illuminate\Foundation\Http\Kernel-&gt;sendRequestThroughRouter() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:116
 34. Illuminate\Foundation\Http\Kernel-&gt;handle() /opt/lampp/htdocs/apidemo/public/index.php:55
 35. require_once() /opt/lampp/htdocs/apidemo/server.php:21

1 个答案:

答案 0 :(得分:1)

在api.php文件中更改此行

Route::resource('articles', 'ArticleController/show');

到这个

Route::resource('articles', 'ArticleController');

并运行

php artisan route:cache

命令。