简体   繁体   English

Symfony fosrestbundle网址格式

[英]Symfony fosrestbundle url formming

My route file look like 我的路线文件看起来像

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

Api method defined as: 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;
    }

I'm getting url: 我正在获取网址:

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

Want to have: /api/user/{id} 想要拥有:/ api / user / {id}

How I can fix that without adding @Route into annotation, because I'm using auto route naming. 如何在不将@Route添加到注释中的情况下解决此问题,因为我使用的是自动路由命名。

Instead of using the "name_prefix" you can just use "prefix" in routing.yml . 除了使用“ name_prefix”,您还可以在routing.yml使用“ prefix”。

with the method 与方法

public function getUserAction($id) {}

and the configuration inside MyBundle/Resources/config/routing.yml : 以及MyBundle/Resources/config/routing.yml

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

I get the generated route: 我得到了生成的路线:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM