具有RoutingAutoBundle(Symfony CMF)的嵌套路由 [英] Nested Routes with RoutingAutoBundle (Symfony CMF)
问题描述
我想这很简单.我只想在嵌套页面上使用RoutingAutoBundle.
I imagine this is a simple enough thing. I just want to use the RoutingAutoBundle for nested pages.
我在这里关注 http://symfony.com/doc/current/cmf/cookbook/creating_a_cms/
说我有具有父项的Page
个文档.
Say I have Page
documents which have a parent.
/**
*
* @PHPCR\Document(referenceable=true)
*
* @author Matt Durak <mattdurak@gmail.com>
*/
class Page implements RouteReferrersReadInterface
{
/**
* @PHPCR\Id()
*/
protected $id;
/**
* @PHPCR\ParentDocument()
*/
protected $parent;
//...
/**
* Get ID
*
* @return integer
*/
public function getId()
{
return $this->id;
}
public function getParent()
{
return $this->parent;
}
public function setParent($parent)
{
$this->parent = $parent;
return $this;
}
// ...
}
我的自动路由配置如下:
My auto routing configuration is like so:
cmf_routing_auto:
mappings:
Study\MainBundle\Document\Page:
content_path:
pages:
provider: [specified, { path: /cms/routes/page }]
exists_action: auto_increment
not_exists_action: create
content_name:
provider: [content_method, { method: getTitle }]
exists_action: auto_increment
not_exists_action: create
我想要类似以下的内容.假设我的数据是这样的:
I would like something like the following. Assume I have my data like so:
/cms/pages
/page-1
/page-2
/page-A
/page-B
当前,这4个页面的路线如下
Currently, those 4 pages would have the following routes
/page/page-1
/page/page-2
/page/page-A
/page/page-B
我想要
/page/page-1
/page/page-2
/page/page-2/page-A
/page/page-2/page-B
我尝试使用content_object
提供程序添加另一个content_path
并调用getParent
,但这没有用.是否有人熟悉Symfony CMF和RoutingAutoBundle,知道如何执行此操作?文档稀疏...
I've tried adding another content_path
with the content_object
provider and calling getParent
, but that did not work. Is anyone familiar with Symfony CMF and the RoutingAutoBundle that knows how to do this? Documentation is sparse...
推荐答案
您可以使用content_method提供程序,并在类中返回null或父级.从RoutingAutoBundle alpha10开始,允许提供程序不向路径添加任何内容.
You can use the content_method provider and return either null or the parent in the class. As of RoutingAutoBundle alpha10, a provider is allowed to not add anything to the path.
代码如下:
cmf_routing_auto:
mappings:
Study\MainBundle\Document\Page:
content_path:
pages:
provider: [specified, { path: /cms/routes/page }]
exists_action: auto_increment
not_exists_action: create
parent:
provider: [content_method, { method: getParentPath }]
exists_action: use
not_exists_action: create
content_name:
provider: [content_method, { method: getTitle }]
exists_action: auto_increment
not_exists_action: create
class Page
{
// ...
public function getParentPath()
{
return $this->parent instanceof static ? $this->parent->getTitle() : null;
}
}
您还可以使用content_object,但计划将其从捆绑软件中删除.
You could also use content_object, but that one is planned to be removed from the bundle.
这篇关于具有RoutingAutoBundle(Symfony CMF)的嵌套路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!