【问题标题】:How to add a general URI prefix to all routes of a controller with FOSRestBundle?如何使用 FOSRestBundle 为控制器的所有路由添加通用 URI 前缀?
【发布时间】:2017-02-25 17:36:36
【问题描述】:

我已经开始为使用 Symfony 开发的应用程序实现控制器。这是我第一次尝试在该任务中同时使用 Symfony 和 PHP:我通常使用 Java,以及 JAX-RS 或 Spring。我关注了this tutorial

我的测试类如下,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”为前缀,这样客户端就可以使用/tags,而不是/api/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 返回的路由集与 php bin/console debug:router 不同的事实证实了这一点(环境规范已删除)。清除缓存后对路由或方法的更改在浏览器中生效:php bin/console cache:clear --env=prod

【问题讨论】:

    标签: php symfony fosrestbundle


    【解决方案1】:

    你可以在你的类中使用路由注解,比如:

    /**
     * @Route("/api")
     */
    class TagController extends FOSRestController
    {
        ...
    

    【讨论】:

    • 你救了我头上的头发。我将注释变成了@Rest\Route("/api"),以避免另一个use 指令。然而,php bin/console debug:router(我使用 Symfony 3.0)会立即获取对路由前缀或函数路由片段的任何更改,但在新路由上发出 GET 总是返回 404。
    • 这实际上对我不起作用。代码几乎相同。我有 symfony/symfony 3.2.* 和 friendsofsymfony/rest-bundle 2.2
    【解决方案2】:

    为我工作:

    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 - 一切正常。

    【讨论】:

    • 把“/create”放在url里是不平静的。你真的不需要。
    【解决方案3】:

    在我的情况下,使用 @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}         
     -------------------------- -------- -------- ------ ---------------------------------------- 
    

    【讨论】:

    • 如果我需要在类前缀中添加一些参数怎么办?例如:/resource/{index} 并为参数定义一些要求。就我而言:\w+
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-29
    • 1970-01-01
    • 1970-01-01
    • 2019-06-24
    • 1970-01-01
    • 2017-08-01
    相关资源
    最近更新 更多