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:
@ApiResource(itemOperations={ * "post", * "special"={"route_name"="user_method_special"} * }) */ class Users
User Controller:
class UserController extends Controller implements AuthenticatedController { public function userMethod(Request $request) { $userId = $request->get('user_id'); $method = $request->get('method'); $em = $this->getDoctrine()->getManager(); $userRepository = $em->getRepository(Users::class); $user = $userRepository->findOneBy(['userId' => $userId]); $userModel = new UserModel($em); if (!$method) { if (!$user) { return new JsonResponse(['message' => 'Brak oraz userId: ' . $userId], Response::HTTP_NOT_FOUND, ['content-type'=>'application/json']); } $method = $userModel->getMethod($userId); } $method = $userModel->parseMethod($method, $acclen, $userId); //return new JsonResponse([ 'data' => FormatModel::formatMethod($method, $format)], Response::HTTP_OK, ['content-type'=>'application/json']); return FormatModel::formatMethod($method, $format); } }
routes.yaml:
user_method: path: '/user-method' methods: ['POST'] defaults: _controller: '\App\Controller\UserController::userMethod' _api_resource_class: 'App\Entity\Users' _api_item_operation_name: 'special'
Co robię źle?