Manual definition of routes

2.0 version
Maintained

Manual definition of routes

If the automatic route generation does not fit your needs, you can manually define a route using simple annotations. This is very helpful if you want to have more than one url parameter without having a static word in between them.

For a full list of annotations check out FOS/RestBundle/Controller/Annotations:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Delete Route Definition
use FOS\RestBundle\Controller\Annotations\Delete;

/**
 * DELETE Route annotation.
 * @Delete("/likes/{type}/{typeId}")
 */

// Head Route Definition
use FOS\RestBundle\Controller\Annotations\Head;

/**
 * HEAD Route annotation.
 * @Head("/likes/{type}/{typeId}")
 */

// Get Route Definition
use FOS\RestBundle\Controller\Annotations\Get;

/**
 * GET Route annotation.
 * @Get("/likes/{type}/{typeId}")
 */

// Patch Route Definition
use FOS\RestBundle\Controller\Annotations\Patch;

/**
 * PATCH Route annotation.
 * @Patch("/likes/{type}/{typeId}")
 */

// Options Route Definition
use FOS\RestBundle\Controller\Annotations\Options;

/**
 * OPTIONS Route annotation.
 * @Options("/likes/{type}/{typeId}")
 */


// Post Route Definition
use FOS\RestBundle\Controller\Annotations\Post;

/**
 * POST Route annotation.
 * @Post("/likes/{type}/{typeId}")
 */

// Put Route Definition
use FOS\RestBundle\Controller\Annotations\Put;

/**
 * PUT Route annotation.
 * @Put("/likes/{type}/{typeId}")
 */

Method Name Prefix

By default, the routing name defined by the annotation is appended to the generated routing name.

Example:

1
2
3
4
5
6
7
use FOS\RestBundle\Controller\Annotations\Get

/**
* @Get("/users/foo", name="_foo")
* @Get("/users/bar", name="_bar")
*/
public function getUsersAction() { /** */ }

Result:

Name Method Scheme Host Path
get_users_foo GET ANY ANY /users/foo.{_format}
get_users_bar GET ANY ANY /users/bar.{_format}

You can add the method_prefix option to change this behavior.

Example:

1
2
3
4
5
6
7
use FOS\RestBundle\Controller\Annotations\Get

/**
* @Get("/users/foo", name="get_foo", options={ "method_prefix" = false })
* @Get("/users/bar", name="get_bar", options={ "method_prefix" = false })
*/
public function getUsersAction() { /** */ }

Result:

Name Method Scheme Host Path
get_foo GET ANY ANY /users/foo.{_format}
get_bar GET ANY ANY /users/bar.{_format}

Or you can disable it globally by setting:

1
2
3
...
routing_loader:
    prefix_methods: false

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.