Bonjour,
je suis entrain de developer une API avec Slim3 et quelques composents Illuminate (database, http, pagination).
Database pour Eloquent,
Http pour le systeme de Resources Json.
Mon probleme est le suivant :
quand j'utilise le systeme de Resources sur le resultat de la query :
#controllers/Posts.php
namespace App\Controllers;
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
use App\Resources\Post\PostsResource;
use App\Models\Post;
class Posts
{
public function index(Request $request, Response $response)
{
$posts = new PostsResource(Post::all());
}
}
#resources/PostsResource.php
namespace App\Resources\Post;
use Illuminate\Http\Resources\Json\ResourceCollection;
use Illuminate\Support\Collection;
class PostsResource extends ResourceCollection
{
public function toArray($request)
{
return [
'data' => PostResource::collection($this->collection),
];
}
}
#resources/PostResource.php
namespace App\Resources\Post;
use Illuminate\Http\Resources\Json\Resource;
class PostResource extends Resource
{
public function toArray($request)
{
return [
'type' => 'posts',
'id' => (string)$this->id,
'attributes' => [
'title' => $this->title,
'body' => $this->body,
],
'relationships' => new PostRelationshipResource($this)
];
}
}
#resources/PostRelationshipResource.php
namespace App\Resources\Post;
use Illuminate\Http\Resources\Json\Resource;
class PostRelationshipResource extends Resource
{
public function toArray($request)
{
return [
'author' = [
'data' => []
]
];
}
}
j'ai cette erreur qui ce produit :
type "ReflectionException",
message "Class request does not exist",
file "vendor/illuminate/container/Container.php",
line 767
mais que je ne l'utilise pas, tout ce passe bien.
Je ne sais pas trop quoi faire. Je vais faire sans le systeme de Resources pour l'instant.
Si il vous faut d'autres informations.
Merci
Hello, il est vrai que l'erreur n'est pas forcément parlante concernant ton souci.
À la relecture, tu instancies PostsResource alors que ta classe se nomme PostResource. Le problème ne serait pas celui-ci ?
Ah oui effectivement j'ai fait une erreur de copier/coller.
J'ai la trace de l'erreur si ca peut aider un peu plus.
0 "/Users/JulienMiclo/WebServer/sandbox/grobill/vendor/illuminate/container/Container.php(767): ReflectionClass->__construct('request')"
1 "/Users/JulienMiclo/WebServer/sandbox/grobill/vendor/illuminate/container/Container.php(646): Illuminate\\Container\\Container->build('request')"
2 "/Users/JulienMiclo/WebServer/sandbox/grobill/vendor/illuminate/container/Container.php(601): Illuminate\\Container\\Container->resolve('request', Array)"
3 "/Users/JulienMiclo/WebServer/sandbox/grobill/vendor/illuminate/http/Resources/Json/JsonResource.php(204): Illuminate\\Container\\Container->make('request')"
4 "[internal function]: Illuminate\\Http\\Resources\\Json\\JsonResource->jsonSerialize()"
5 "/Users/JulienMiclo/WebServer/sandbox/grobill/vendor/slim/slim/Slim/Http/Response.php(349): json_encode(Array, 0)"
6 "/Users/JulienMiclo/WebServer/sandbox/grobill/config/initializers/functions.php(15): Slim\\Http\\Response->withJson(Array)"
7 "/Users/JulienMiclo/WebServer/sandbox/grobill/app/controllers/Posts.php(19): response(Object(Slim\\Http\\Response), Array)"
8 "[internal function]: App\\Controllers\\Posts->index(Object(Slim\\Http\\Request), Object(Slim\\Http\\Response), Array)"
9 "/Users/JulienMiclo/WebServer/sandbox/grobill/vendor/slim/slim/Slim/Handlers/Strategies/RequestResponse.php(41): call_user_func(Array, Object(Slim\\Http\\Request), Object(Slim\\Http\\Response), Array)"
10 "/Users/JulienMiclo/WebServer/sandbox/grobill/vendor/slim/slim/Slim/Route.php(335): Slim\\Handlers\\Strategies\\RequestResponse->__invoke(Array, Object(Slim\\Http\\Request), Object(Slim\\Http\\Response), Array)"
11 "/Users/JulienMiclo/WebServer/sandbox/grobill/vendor/slim/slim/Slim/MiddlewareAwareTrait.php(117): Slim\\Route->__invoke(Object(Slim\\Http\\Request), Object(Slim\\Http\\Response))"
12 "/Users/JulienMiclo/WebServer/sandbox/grobill/vendor/slim/slim/Slim/Route.php(313): Slim\\Route->callMiddlewareStack(Object(Slim\\Http\\Request), Object(Slim\\Http\\Response))"
13 "/Users/JulienMiclo/WebServer/sandbox/grobill/vendor/slim/slim/Slim/App.php(513): Slim\\Route->run(Object(Slim\\Http\\Request), Object(Slim\\Http\\Response))"
14 "/Users/JulienMiclo/WebServer/sandbox/grobill/vendor/slim/slim/Slim/MiddlewareAwareTrait.php(117): Slim\\App->__invoke(Object(Slim\\Http\\Request), Object(Slim\\Http\\Response))"
15 "/Users/JulienMiclo/WebServer/sandbox/grobill/vendor/slim/slim/Slim/App.php(406): Slim\\App->callMiddlewareStack(Object(Slim\\Http\\Request), Object(Slim\\Http\\Response))"
16 "/Users/JulienMiclo/WebServer/sandbox/grobill/vendor/slim/slim/Slim/App.php(314): Slim\\App->process(Object(Slim\\Http\\Request), Object(Slim\\Http\\Response))"
17 "/Users/JulienMiclo/WebServer/sandbox/grobill/config/boot.php(22): Slim\\App->run()"
18 "/Users/JulienMiclo/WebServer/sandbox/grobill/public/index.php(3): require('/Users/JulienMi...')"
19 "/Users/JulienMiclo/WebServer/sandbox/grobill/index.php(3): require('/Users/JulienMi...')"
20 "{main}"