Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [SF][Symfony4][Api-platform]Api-platform, jak dodać własną metodę z controllera
Crash89
post 4.07.2018, 16:46:48
Post #1





Grupa: Zarejestrowani
Postów: 191
Pomógł: 7
Dołączył: 3.04.2013

Ostrzeżenie: (0%)
-----


Witam,

Chciałbym w Api-platform dla symfony 4 wykonać metodę z controllera, gdy robię to według dokumentacji api-platform to mi nie działa, gdy wykonam to dla operacji GET to mam taki błąd:

NotFoundHttpException Not Found

Kiedy wykonuję to dla operacji POST mam taki błąd:

An exception has been set up for the application of the application. (Either a route). config / routes / api_platform.yaml). Make sure there is a loader supporting the api_platform type

Poniżej przedstawię kod:

  1. @ApiResource(itemOperations={
  2. * "post",
  3. * "special"={"route_name"="user_method_special"}
  4. * })
  5. */
  6. class Users



User Controller:

  1. class UserController extends Controller implements AuthenticatedController
  2. {
  3.  
  4. public function userMethod(Request $request)
  5. {
  6. $userId = $request->get('user_id');
  7. $method = $request->get('method');
  8.  
  9. $em = $this->getDoctrine()->getManager();
  10. $userRepository = $em->getRepository(Users::class);
  11. $user = $userRepository->findOneBy(['userId' => $userId]);
  12.  
  13. $userModel = new UserModel($em);
  14.  
  15. if (!$method) {
  16.  
  17. if (!$user) {
  18. return new JsonResponse(['message' => 'Brak oraz userId: ' . $userId], Response::HTTP_NOT_FOUND, ['content-type'=>'application/json']);
  19. }
  20. $method = $userModel->getMethod($userId);
  21. }
  22.  
  23. $method = str_replace(' ', '', $method);
  24. $acclen = strlen($method);
  25.  
  26. $method = $userModel->parseMethod($method, $acclen, $userId);
  27.  
  28. //return new JsonResponse([ 'data' => FormatModel::formatMethod($method, $format)], Response::HTTP_OK, ['content-type'=>'application/json']);
  29. return FormatModel::formatMethod($method, $format);
  30. }
  31. }

routes.yaml:

  1. user_method:
  2. path: '/user-method'
  3. methods: ['POST']
  4. defaults:
  5. _controller: '\App\Controller\UserController::userMethod'
  6. _api_resource_class: 'App\Entity\Users'
  7. _api_item_operation_name: 'special'


Co robię źle?

Ten post edytował Crash89 4.07.2018, 16:50:36
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 28.03.2024 - 20:16