【问题标题】:Symfony CMF Dynamic Router no contentDocumentSymfony CMF 动态路由器 no contentDocument
【发布时间】:2018-05-21 13:47:21
【问题描述】:

按照Dynamic Router 文档,我可以:

  • 创建链接到我的内容的路由(页面实体)
  • 用 ORM 坚持下去
  • 加载我的控制器匹配路由

但是由于我的控制器应该期望 $​​contentDocument 参数,我什么都没有。

以下是我创建路线和实体的方式:

    $page = new Page();
    $page->setTitle('My Content')
    ->setBackground(1)
    ->setDescription('My description')
    ->setContent('<h1>Hello</h1>');
    $manager->persist($page);
    $manager->flush(); // flush to be able to use the generated id

    $contentRepository = $this->container->get('cmf_routing.content_repository');

    $route = new Route();
    $route->setName('my-content');
    $route->setStaticPrefix('/my-content');
    $route->setDefault(RouteObjectInterface::CONTENT_ID, $contentRepository->getContentId($page));
    $route->setContent($page);
    $page->addRoute($route); // Create the backlink from content to route

    $manager->persist($page);
    $manager->flush();

这是我的配置部分:

cmf_routing:
    chain:
        routers_by_id:
            router.default: 200
            cmf_routing.dynamic_router: 100
    dynamic:
        enabled: true
        persistence:
            orm:
                enabled: true
        generic_controller: AppBundle:Default:showPage
        templates_by_class:
            AppBundle\Entity\Page: AppBundle:Default:index.html.twig

我的控制器:

public function showPageAction($page)
{
    return $this->render('default/index.html.twig');
}

还有错误:

Controller "AppBundle\Controller\DefaultController::showPageAction()" 要求您为 "$page" 参数提供一个值。参数可以为空且未提供空值、未提供默认值或因为在此参数之后有一个非可选参数。

【问题讨论】:

    标签: symfony routing symfony-3.4 symfony-cmf


    【解决方案1】:

    动态路由将内容文档放入名为“contentDocument”的请求中。您需要明确使用该名称:

    public function showPageAction($contentDocument)
    {
        return $this->render('default/index.html.twig');
    }
    

    如果您不需要在控制器中执行任何操作,则无需指定 generic_controller。 template_by_class 将使捆绑提供的控制器使用 $contentDocument 中的页面实例呈现该模板。

    【讨论】:

      猜你喜欢
      • 2016-02-07
      • 1970-01-01
      • 1970-01-01
      • 2016-01-16
      • 1970-01-01
      • 1970-01-01
      • 2014-03-16
      • 2019-03-21
      • 1970-01-01
      相关资源
      最近更新 更多