问题
I imagine this is a simple enough thing. I just want to use the RoutingAutoBundle for nested pages.
I'm following along here http://symfony.com/doc/current/cmf/cookbook/creating_a_cms/
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
Currently, those 4 pages would have the following routes
/page/page-1
/page/page-2
/page/page-A
/page/page-B
I would like
/page/page-1
/page/page-2
/page/page-2/page-A
/page/page-2/page-B
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...
回答1:
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.
The code would look like:
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;
}
}
You could also use content_object, but that one is planned to be removed from the bundle.
来源:https://stackoverflow.com/questions/21816591/nested-routes-with-routingautobundle-symfony-cmf