gha*_*ari 7 symfony fosrestbundle symfony-2.7
我有三个名为控制器BlogController,PostController,CommentController即CommentController是子资源PostController和PostController子资源BlogController.
/**
* @Rest\RouteResource("blog", pluralize=false)
*/
class BlogController extends FOSRestController
{
public function getAction($blogUri)
{
...
}
}
/**
* @Rest\RouteResource("post", pluralize=false)
*/
class PostController extends FOSRestController
{
public function getAction($postId)
{
...
}
}
/**
* @Rest\RouteResource("comment", pluralize=false)
*/
class CommentController extends FOSRestController
{
public function getAction($commentId)
{
...
}
}
Run Code Online (Sandbox Code Playgroud)
使用routing.yml
mgh_blog:
resource: MGH\BlogBundle\Controller\BlogController
type: rest
mgh_blog_post:
resource: MGH\BlogBundle\Controller\PostController
type: rest
parent: mgh_blog
mgh_blog_post_comment:
resource: MGH\PostBundle\Controller\CommentController
type: rest
parent: mgh_blog_post
Run Code Online (Sandbox Code Playgroud)
我定义 getAction方法,但我得到以下错误:
[InvalidArgumentException]
Every parent controller must have `get{SINGULAR}Action($id)` method
where {SINGULAR} is a singular form of associated object
Run Code Online (Sandbox Code Playgroud)
编辑:
我也尝试该方法的名称更改为getCommentAction($commentId),getPostAction($postId)和getBlogAction,但它没有工作.
当我使用@RouteResource注释时,方法名称必须是getAction($id),否则它不起作用.
当我改变mgh_blog_post_comment路由器的父级时mgh_blog,它正在工作!
这个错误描述非常糟糕,浪费时间,因为它没有告诉你真正的问题是什么.请尝试以下方法:
/**
* @Rest\RouteResource("blog", pluralize=false)
*/
class BlogController extends FOSRestController
{
public function getAction($blogUri)
{
...
}
}
/**
* @Rest\RouteResource("post", pluralize=false)
*/
class PostController extends FOSRestController
{
public function getAction($blogUri, $postId)
{
...
}
}
/**
* @Rest\RouteResource("comment", pluralize=false)
*/
class CommentController extends FOSRestController
{
public function getAction($blogUri, $postId, $commentId)
{
...
}
}
Run Code Online (Sandbox Code Playgroud)
您在后代控制器操作中没有正确数量的参数.我花了两天的步调试来解决这个问题.
父路线,博客,看起来像:
/blog/{blogUri}
Run Code Online (Sandbox Code Playgroud)
它会匹配
public function getAction($blogUri)
Run Code Online (Sandbox Code Playgroud)
儿童路线Post,看起来像:
/blog/{blogUri}/post/{postId}
Run Code Online (Sandbox Code Playgroud)
它与下面的代码不匹配,因为它需要两个参数.对于孙子来说也是如此 - 它正在寻找三个参数:
public function getAction($postId)
Run Code Online (Sandbox Code Playgroud)
孙子路线,评论,看起来像:
/blog/{blogUri}/post/{postId}/comment/{commentId}
Run Code Online (Sandbox Code Playgroud)
代码跟踪每个控制器的祖先.这篇文章有1个祖先.在为post控制器构建路由时,代码会查看"get action"上的参数数量.它取参数的数量并减去祖先的数量.如果差异不等于1,则会抛出错误.
结论,对于每个后代,它需要包括它的祖先的ID参数和它自己的ID.应该总是有一个参数比祖先更多.
| 归档时间: |
|
| 查看次数: |
996 次 |
| 最近记录: |