zenstruck/content-bundle

该包已被废弃,不再维护。未建议替代包。

使用 Doctrine2 类表继承的 Symfony2 简单 CMS

安装: 263

依赖: 0

建议者: 0

安全: 0

星标: 13

关注者: 1

分支: 1

开放问题: 0

类型:symfony-bundle

v1.3.5 2013-12-11 15:22 UTC

This package is auto-updated.

Last update: 2020-10-28 14:15:44 UTC


README

Build Status

此 Bundle 允许使用 Doctrine2 的继承功能实现各种 内容类型(有关更多信息,请参阅http://www.doctrine-project.org/docs/orm/2.1/en/reference/inheritance-mapping.html)。它允许所有内容类型继承自单个 Node。Doctrine2 的实现问题在于它要求你在最顶层的实体中设置所有继承的实体。使用此 Bundle,它们可以在你的 config.yml 中设置。

配置

  1. 创建一个 Node

    // path/to/your/bundle/Entity/Node.php
    
    namespace YourApplicationBundle\Entity;
    
    use Zenstruck\Bundle\ContentBundle\Entity\Node as BaseNode;
    
    /**
     * @orm:Entity
     */
    class Node extends BaseNode
    {
        // add any node fields (or leave empty)
    }
  2. 创建一个或多个内容类型实体(继承自你的 Node 实体)

    // path/to/your/bundle/Entity/BlogPost.php
    
    namespace YourApplicationBundle\Entity;
    
    /**
     * @orm:Entity
     */
    class BlogPost extends Node
    {
        /**
         * @orm:Column(type="text", nullable=true)
         */
        protected $body;
    
        public function getBody()
        {
            return $this->body;
        }
    
        public function setBody($body)
        {
            $this->body = $body;
        }
    }

    注意:它们也可以相互继承(BlogPost->Page->Node

  3. 将你的节点类和任何新的内容类型添加到你的 config.yml

    zenstruck_content:
            node_class: YourApplicationBundle\Entity\Node
        content_types:
            blog_post:  YourApplicationBundle\Entity\BlogPost
            ...

    注意:在上面的示例中,类 BlogPost 的机器名称是 blog_post。这种命名约定很重要。

  4. (可选)要使用此 Bundle 提供的控制器,请在你的 config.yml 中激活它

    zenstruck_content:
        use_controller: true
  5. (可选)如果你在第 4 步中使用了控制器,请添加路由

    zenstruck_content:
        resource: "@ZenstruckContentBundle/Resources/config/routing.xml"

参考

管理器

存在一个管理类,可以通过服务容器的 zenstruck_content.manager ID 获取。

面包屑

管理器包含一个名为 getAncestors(Node $node) 的函数。该函数根据当前 Node 的路径返回一个祖先节点数组。

例如,如果你传递一个路径为 foo/bar/baz 的节点,它将返回一个包含路径为 foofoo/bar 的节点数组,如果它们存在且按此顺序。

用法

// controller
$manager = $this->container->get('zenstruck_content.manager');
$manager->getAncestors($node);

继承类型

默认情况下使用 Doctrine2 的 类表继承。这意味着从 Node 继承的每个内容类型都是其自己的表,并链接回基本 node 表。还有一个选项使用 类表继承。所有内容类型都将存储在同一表中。

你可以在你的 config.yml 中启用此功能

zenstruck_content:
    inheritance_type: single_table

模板

要提供自己的模板,请在你的 config.yml 中设置 default_template 选项

zenstruck_content:
    default_template: YourApplicationBundle:Content:node.html.twig

注意:默认模板名称必须是 node

继承唯一实体约束

本捆绑包包含一个自定义的 UniqueEntity 验证约束。默认的Doctrine约束在继承方面存在问题。它只检查其当前作用域和子实体的值。例如,如果您有如下结构:BlogPost->Page->Node,并将默认的Doctrine UniqueEntity 约束放置在 Page 字段中。保存一个字段与 Page 中的字段相同的 BlogPost 不会使约束失效。

本捆绑包附带的 InheritedUniqueEntity 约束可以做到这一点。

使用方法

以下示例演示了在 Page 实体的 body 字段上添加 UniqueEntity 约束。所有继承自 Page 的类都将具有此约束在 Page 范围内。

namespace Acme\DemoBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Zenstruck\Bundle\ContentBundle\Validator\InheritedUniqueEntity;

/**
 * Acme\DemoBundle\Entity\Node
 *
 * @ORM\Table(name="page")
 * @ORM\Entity
 * @InheritedUniqueEntity(field="body")
 */
class Page extends Node
{

    /**
     * @var string $body
     *
     * @ORM\Column(name="body", type="string", length=255, nullable=true)
     */
    protected $body;

    //...
}

生成网站地图

本捆绑包附带了用于与 DpnXmlSitemapBundle 一起使用的网站地图生成器

使用方法

  1. 安装并配置 DpnXmlSitemapBundle

  2. 在您的 config.yml 中启用网站地图生成器

    zenstruck_content:
        sitemap:
            enabled: true
  3. 默认情况下,生成器使用 Node 实体管理器的 findAll 方法。要使用不同的方法,请在您的 config.yml 中更改它

    zenstruck_content:
        sitemap:
            entity_manager_method: myCustomMethod

网站地图应可在 /sitemap.xml 中找到。

完整的默认配置

zenstruck_content:
    node_class:           ~ # Required
    node_type_name:       node
    manager_class:        ~
    use_controller:       false
    use_form:             false
    inheritance_type:     class_table
    discriminator_column: content_type
    default_template:     ZenstruckContentBundle:Node:node.html.twig
    content_types: []
    sitemap:
        enabled:                false
        entity_manager_method:  findAll