dongni8969 2018-07-03 17:57
浏览 46

Api平台,在route_name参数中输入什么?

I would like to write my own controller in the api-platform for symfony 4. There is an error with the GET operation:  

NotFoundHttpException HTTP 404 Not Found Not Found

And with the post operation I have an error:

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.

here's the code:

    @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);
        }
}

and 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'

what am I doing wrong?

I would like to return one value taken from doctrine.

Edit: When I changed the definition route_name = user_method, I have an error with the POST method:

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.)

  • 写回答

2条回答 默认 最新

  • douzhongjiu2263 2018-07-03 18:00
    关注

    The code above is variable $ account. Instead of it, there should be a $ method and the $replaceZeros variable should not be at all.

    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP