@RouteResource不起作用

Cri*_*jon 10 php symfony fosrestbundle

我正在使用FosRestBundle,我正在使用手动路由声明一个控制器.

namespace Cboujon\PropertyBundle\Controller;

use FOS\RestBundle\Controller\Annotations\QueryParam;
use FOS\RestBundle\Controller\Annotations\RouteResource;
use FOS\RestBundle\View\View;
use FOS\RestBundle\Controller\Annotations\Get;


/**
 * Property controller.
 * @RouteResource("Property")
 */
class PropertyRESTController extends \FOS\RestBundle\Controller\FOSRestController {


    /**
     * 
     * @return type
     * @Get("/types")
     * 
     */
    public function getTypesAction() {
        ...
        return $this->get('fos_rest.view_handler')->handle($view);
    }

}
Run Code Online (Sandbox Code Playgroud)

使用routing.yml

cboujon_property_property_api:
    resource: "@CboujonPropertyBundle/Controller/PropertyRESTController.php"
    type:     rest
    prefix:   /api
Run Code Online (Sandbox Code Playgroud)

当我提出要求http://localhost:8000/api/properties/types或者http://localhost:8000/api/property/types我得到了

找不到404 - NotFoundHttpException.

但如果http://localhost:8000/api/types它有效!

我需要配置网址http://localhost:8000/api/properties/types.我究竟做错了什么?

更新1

没有Property实体.PropertyController应该执行自定义操作(无CRUD).

更新2

你可以看到git存储库

gio*_*mhz 9

您根本无法在FOSRestBundle中混合隐式和显式路由生成(针对单个路由).

@RouteResource用于在隐式路由生成期间"前缀",而@Get用于显式路由.

此外,隐式路由旨在加速标准CRUD资源编辑,因此不是您的情况:只需进行显式路由并避免所有复杂情况.您仍然可以从其他FOSRestBundle功能中受益,例如View handler,@ParamFetchers,...

为什么

与隐式路线生成,所述路由是从方法名中提取(例如postTypeAction变得像POST /type, cgetTypeAction成为像GET /types).

如果你选择了明确的方法,你可以使用@Route,@Get,@Post,...注释,设置为任何你想要的资源URL.使用显式路由, @RouteResource没有意义; 只需使用标准的Symfony前缀.

@RouteResource另一方面,注释是可以自定义路径资源名称(例如get_RESOURCE_myaction).

调试你的代码

为了澄清,这里有一些app/console debug:router代码输出(我已经对你的项目应用了一些语法修复来运行这个命令).

注意:我已设置前缀/api/prefix以避免混淆

@RouteResource+ @Get(这解释了为什么会出现404错误):

 Name                     Method Scheme Host Path                              
 get_property_types       GET    ANY    ANY  /api/prefix/types.{_format}   
Run Code Online (Sandbox Code Playgroud)

@RouteResource 仅(注意隐式命名发生):

 Name                     Method Scheme Host Path                              
 get_property_types       GET    ANY    ANY  /api/prefix/property/types.{_format}
Run Code Online (Sandbox Code Playgroud)

@Get仅(注意它与您当前的场景相同,只是路由名称更改,因为未设置@Get):

 Name                     Method Scheme Host Path                              
 get_types                GET    ANY    ANY  /api/prefix/types.{_format}
Run Code Online (Sandbox Code Playgroud)

删除两者(仍然是相同的,但是,因为调用方法,这只是一个巧合getTypesAction):

 Name                     Method Scheme Host Path                              
 get_types                GET    ANY    ANY  /api/prefix/types.{_format}       
Run Code Online (Sandbox Code Playgroud)

一个非主题笔记

在OOP中,static abstract function无法定义.一个static方法是在类级别上定义的,所以没有多态性可能发生,因为PHP不能提前哪个子类是使用知道.当该方法不是静态的时,PHP知道对象类(因为可以访问$this),并且您可以具有多态行为.

在您的班级中,Cboujon\PropertyBundle\Entity\Property您需要删除该方法static abstract function getLabel或将其定义为abstract function getLabel.