Я хотел бы написать свой собственный контроллер на api-платформе для Symfony 4. Ошибка операции GET:
NotFoundHttpException HTTP 404 Not Found Not Found
И при почтовой операции у меня ошибка:
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.
вот код:
@ApiResource(itemOperations = {
* "post",
* "special" = {"route_name" = "user_method_special"}
* })
*/
class Users
UserController:
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']);
}
$account = $userModel->getMethod($userId);
}
$method = str_replace(' ', '', $method);
$acclen = strlen($method);
$account = $userModel->parseMethod($method, $acclen, $userId, $replaceZeros);
//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'
Что я делаю неправильно?
Я хотел бы вернуть одно значение, взятое из doctrine.
Редактировать: Когда я изменил определение route_name = user_method, у меня возникла ошибка с методом POST:
Exception thrown when handling an exception (Symfony\Component\Config\Exception\FileLoaderLoadException: Either a "route_name" or a "method" operation attribute must exist for the operation "post" of the resource "App\Entity\Users" in . (which is being imported from "D:\xampp\htdocs\api\config/routes/api_platform.yaml"). Make sure there is a loader supporting the "api_platform" type.)






Приведенный выше код - переменная $ account. Вместо него должен быть метод $, а переменной $ replaceZeros не должно быть вообще.
Имя маршрута в определении вашей операции и в определении маршрутизации не совпадает:
user_method_специальный против user_method
Кстати, метод, который вы используете для создания пользовательских операций, не рекомендуется: https://api-platform.com/docs/core/operations/#recommended-method