简体   繁体   中英

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:

    /**
     * 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}

How I can fix that without adding @Route into annotation, because I'm using auto route naming.

Instead of using the "name_prefix" you can just use "prefix" in routing.yml .

with the method

public function getUserAction($id) {}

and the configuration inside 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}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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