它被称为apath variable:
路径变量使我们能够添加动态路由。
参见以下示例,摘自REST API handbook:
// Here we are registering our route for single products. The (?P<id>[\\d]+) is
// our path variable for the ID, which, in this example, can only be some form
// of positive number.
register_rest_route( \'my-shop/v1\', \'/products/(?P<id>[\\d]+)\', array(
...
\'callback\' => \'prefix_get_product\',
) );
需要注意的重要部分是,在我们注册的第二条路由(上面的一条)中,我们添加了一个path变量
/(?P<id>[\\d]+) 到我们的资源路径
/products. path变量是一个正则表达式。在这种情况下,它使用
[\\d]+ 表示应该是任何数字字符至少一次。如果您对资源使用数字ID,那么这是如何使用path变量的一个很好的示例。当使用路径变量时,我们现在必须小心什么是可以匹配的,因为它是用户输入。
格式为:?P<{name}>{regex pattern}.
根据以上示例,可以转到http://example.com/wp-json/my-shop/v1/products/123 检索单个资源。
附言:我编辑主要是为了添加上面的第二段