如何使用 FOSRestBundle 为控制器的所有路由添加通用 URI 前缀?
How to add a general URI prefix to all routes of a controller with FOSRestBundle?
我已经开始为使用 Symfony 开发的应用程序实现控制器。这是我第一次尝试同时使用 Symfony 和 PHP 完成该任务:我通常使用 Java,同时使用 JAX-RS 或 Spring。我关注了 this tutorial.
我的测试 class 如下,URI /tags
按预期工作:
namespace AppBundle\Controller;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Controller\FOSRestController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class TagController extends FOSRestController
{
/**
* @Rest\Get("/tags")
*/
public function getTagsAction(Request $request)
{
$data = ['getTagsAction' => 'not implemented yet'];
$view = $this->view($data, Response::HTTP_OK);
return $view;
}
}
我希望仅使用注释,所有路由自动以 "api" 为前缀,这样客户将使用 /api/tags
.[=27= 而不是 /tags
]
是否可以定义这样一个通用前缀,自动添加到所有函数的路由前?阅读 the documentation,我认为这个 Docblock 添加将是答案:
/**
* @Rest\Prefix("/api")
*/
class TagController extends FOSRestController
{
...
}
然而,this answer 另一个问题似乎赋予 @Prefix
注释不同的含义。
另外,有没有路由缓存?对路线的更改(tag
而不是 tags
)未被接受。我怀疑需要采取行动来通知框架更改路由或添加新控制器。
[编辑] /var/cache
中确实有缓存。我相信控制台选择似乎被浏览器忽略的更改的原因是控制台假定开发环境,而通过浏览器访问通过 app.php
(而不是 app_dev.php
)。 php bin/console debug:router --env=prod
returns 一组不同于 php bin/console debug:router
的路由(环境规范已删除)这一事实证实了这一点。清除缓存后对路由或方法的更改在浏览器中生效:php bin/console cache:clear --env=prod
.
您可以在 class 中使用路由注释,例如:
/**
* @Route("/api")
*/
class TagController extends FOSRestController
{
...
适合我:
use FOS\RestBundle\Controller\Annotations as Rest;
/**
* Class UsersController
* @package AppBundle\Controller
* @Rest\Route("api/user")
*/
class UsersController extends FOSRestController implements ClassResourceInterface
{
/**
* Creates a new User
*
* @Rest\Post("/create")
* @param Request $request
* @return View
*/
public function createAction(Request $request)
{
然后当我访问 /api/user/create
- 一切正常。
使用 @Route
对我来说不起作用。看起来这种方法在 Symfony/FOSRestBundle 的最新版本中已经过时了。但是,@Prefix
在我的情况下完美工作:
use FOS\RestBundle\Controller\Annotations as Rest;
/**
* @Rest\Prefix("api")
*/
class CustomersController {
public function getCustomersAction() {}
public function newCustomersAction() {}
public function getCustomerAction( $slug ) {}
public function editCustomerAction( $slug ) {}
public function removeCustomerAction( $slug ) {}
}
这最终导致 console debug:router
列出:
bash-4.4# bin/console debug:router
-------------------------- -------- -------- ------ ----------------------------------------
Name Method Scheme Host Path
-------------------------- -------- -------- ------ ----------------------------------------
new_customers GET ANY ANY /api/customers/new.{_format}
edit_customer GET ANY ANY /api/customers/{slug}/edit.{_format}
remove_customer GET ANY ANY /api/customers/{slug}/remove.{_format}
get_customers GET ANY ANY /api/customers.{_format}
get_customer GET ANY ANY /api/customers/{slug}.{_format}
-------------------------- -------- -------- ------ ----------------------------------------
我已经开始为使用 Symfony 开发的应用程序实现控制器。这是我第一次尝试同时使用 Symfony 和 PHP 完成该任务:我通常使用 Java,同时使用 JAX-RS 或 Spring。我关注了 this tutorial.
我的测试 class 如下,URI /tags
按预期工作:
namespace AppBundle\Controller;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Controller\FOSRestController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class TagController extends FOSRestController
{
/**
* @Rest\Get("/tags")
*/
public function getTagsAction(Request $request)
{
$data = ['getTagsAction' => 'not implemented yet'];
$view = $this->view($data, Response::HTTP_OK);
return $view;
}
}
我希望仅使用注释,所有路由自动以 "api" 为前缀,这样客户将使用 /api/tags
.[=27= 而不是 /tags
]
是否可以定义这样一个通用前缀,自动添加到所有函数的路由前?阅读 the documentation,我认为这个 Docblock 添加将是答案:
/**
* @Rest\Prefix("/api")
*/
class TagController extends FOSRestController
{
...
}
然而,this answer 另一个问题似乎赋予 @Prefix
注释不同的含义。
另外,有没有路由缓存?对路线的更改(tag
而不是 tags
)未被接受。我怀疑需要采取行动来通知框架更改路由或添加新控制器。
[编辑] /var/cache
中确实有缓存。我相信控制台选择似乎被浏览器忽略的更改的原因是控制台假定开发环境,而通过浏览器访问通过 app.php
(而不是 app_dev.php
)。 php bin/console debug:router --env=prod
returns 一组不同于 php bin/console debug:router
的路由(环境规范已删除)这一事实证实了这一点。清除缓存后对路由或方法的更改在浏览器中生效:php bin/console cache:clear --env=prod
.
您可以在 class 中使用路由注释,例如:
/**
* @Route("/api")
*/
class TagController extends FOSRestController
{
...
适合我:
use FOS\RestBundle\Controller\Annotations as Rest;
/**
* Class UsersController
* @package AppBundle\Controller
* @Rest\Route("api/user")
*/
class UsersController extends FOSRestController implements ClassResourceInterface
{
/**
* Creates a new User
*
* @Rest\Post("/create")
* @param Request $request
* @return View
*/
public function createAction(Request $request)
{
然后当我访问 /api/user/create
- 一切正常。
使用 @Route
对我来说不起作用。看起来这种方法在 Symfony/FOSRestBundle 的最新版本中已经过时了。但是,@Prefix
在我的情况下完美工作:
use FOS\RestBundle\Controller\Annotations as Rest;
/**
* @Rest\Prefix("api")
*/
class CustomersController {
public function getCustomersAction() {}
public function newCustomersAction() {}
public function getCustomerAction( $slug ) {}
public function editCustomerAction( $slug ) {}
public function removeCustomerAction( $slug ) {}
}
这最终导致 console debug:router
列出:
bash-4.4# bin/console debug:router
-------------------------- -------- -------- ------ ----------------------------------------
Name Method Scheme Host Path
-------------------------- -------- -------- ------ ----------------------------------------
new_customers GET ANY ANY /api/customers/new.{_format}
edit_customer GET ANY ANY /api/customers/{slug}/edit.{_format}
remove_customer GET ANY ANY /api/customers/{slug}/remove.{_format}
get_customers GET ANY ANY /api/customers.{_format}
get_customer GET ANY ANY /api/customers/{slug}.{_format}
-------------------------- -------- -------- ------ ----------------------------------------