我在laravel中创建了一个api并按照这篇文章。最后得到一个错误。
文章管理员:
<?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->() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Container/Container.php:767
2. ReflectionClass->__construct() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Container/Container.php:767
3. Illuminate\Container\Container->build() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Container/Container.php:646
4. Illuminate\Container\Container->resolve() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Container/Container.php:601
5. Illuminate\Container\Container->make() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:734
6. Illuminate\Foundation\Application->make() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Routing/Route.php:226
7. Illuminate\Routing\Route->getController() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Routing/Route.php:796
8. Illuminate\Routing\Route->controllerMiddleware() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Routing/Route.php:757
9. Illuminate\Routing\Route->gatherMiddleware() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Routing/Router.php:678
10. Illuminate\Routing\Router->gatherRouteMiddleware() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Routing/Router.php:658
11. Illuminate\Routing\Router->runRouteWithinStack() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Routing/Router.php:642
12. Illuminate\Routing\Router->runRoute() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Routing/Router.php:608
13. Illuminate\Routing\Router->dispatchToRoute() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Routing/Router.php:597
14. Illuminate\Routing\Router->dispatch() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:176
15. Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:30
16. Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}() /opt/lampp/htdocs/apidemo/vendor/fideloper/proxy/src/TrustProxies.php:57
17. Fideloper\Proxy\TrustProxies->handle() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:151
18. Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
19. Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php:31
20. Illuminate\Foundation\Http\Middleware\TransformsRequest->handle() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:151
21. Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
22. Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php:31
23. Illuminate\Foundation\Http\Middleware\TransformsRequest->handle() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:151
24. Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
25. Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php:27
26. Illuminate\Foundation\Http\Middleware\ValidatePostSize->handle() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:151
27. Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
28. Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php:51
29. Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:151
30. Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:53
31. Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:104
32. Illuminate\Pipeline\Pipeline->then() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:151
33. Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter() /opt/lampp/htdocs/apidemo/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:116
34. Illuminate\Foundation\Http\Kernel->handle() /opt/lampp/htdocs/apidemo/public/index.php:55
35. require_once() /opt/lampp/htdocs/apidemo/server.php:21
答案 0 :(得分:1)
在api.php文件中更改此行
Route::resource('articles', 'ArticleController/show');
到这个
Route::resource('articles', 'ArticleController');
并运行
php artisan route:cache
命令。