Symfony fosrestbundle url formming

时间:2016-09-18 19:06:37

标签: php symfony fosrestbundle

我的路线文件看起来像

#routing.yml
user:
    type: rest
    resource:     api.user.controller
    name_prefix:  api_
loan:
    type: rest
    resource:     api.loan.controller
    name_prefix:  api_

Api方法定义为:

    /**
     * Get a single user.
     *
     * @ApiDoc(
     *   output = "AppBundle\Model\User",
     *   statusCodes = {
     *     200 = "Returned when successful",
     *     404 = "Returned when the user is not found"
     *   }
     * )
     *
     * @param int $id the user id
     *
     * @return array
     *
     * @throws NotFoundHttpException when user not exist
     */
    public function getUserAction($id)
    {
        $repo = $this->model->getRepository(User::class);
        $user = $repo->find($id);

        if (!$user instanceof User) {
            throw new NotFoundHttpException('User not found');
        }

        return $user;
    }

我收到了网址:

api_get__user  GET   ANY   ANY   /api/{id}/user

想拥有:/ api / user / {id}

如何在不将@Route添加到注释中的情况下解决这个问题,因为我正在使用自动路由命名。

1 个答案:

答案 0 :(得分:0)

而不是使用" name_prefix"你可以使用"前缀"在routing.yml

使用方法

public function getUserAction($id) {}

以及MyBundle/Resources/config/routing.yml内的配置:

user.controller:
    type: rest
    resource: "@MyBundle/Controller/UserController.php"
    prefix: 'api'

我得到了生成的路线:

get_user GET ANY ANY /api/users/{id}