perfectneeds / content-single-lang-bundle
内容包
Requires
- php: ^5.5.9 || ^7.0
- perfectneeds/media-bundle: ~1.0
- perfectneeds/service-bundle: ~1.0
- symfony/framework-bundle: ^4.4 || ^3.4
README
先决条件
- Symfony 3.4
- PNMediaBundle
- PNServiceBundle
安装
安装是一个快速(我保证!)7步骤的过程
- 使用 composer 下载 PNContentBundle
- 在 AppKernel 中启用 Bundle
- 创建您的 Post 类
- 创建您的 PostRepository 类
- 配置 PNContentBundle
- 导入 PNContentBundle 路由
- 更新您的数据库模式
步骤 1: 使用 composer 下载 PNContentBundle
使用 composer 需求该 Bundle
$ composer require perfectneeds/content-single-lang-bundle "~1.0"
步骤 2: 在 AppKernel 中启用 Bundle
使用 composer 需求该 Bundle
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new PN\MediaBundle\PNMediaBundle(), new \PN\ServiceBundle\PNServiceBundle(), new \PN\ContentBundle\PNContentBundle(), // ... ); }
步骤 3: 创建您的 Post 类
此 Bundle 的目标是持久化一些 Post
类到数据库中。因此,您的第一个任务是创建应用程序的 Post
类。此类可以看起来和表现成您想要的任何样子:添加任何您认为有用的属性或方法。这是 您的 Post
类。
此 Bundle 提供了基础类,这些类已经映射了大多数字段,以便更容易创建您的实体。以下是您如何使用它
- 扩展基础
Post
类(如果您使用任何 doctrine 变体,则来自Entity
文件夹) - 映射
id
字段。它必须受保护,因为它是从父类继承的。
注意!
当您从 Bundle 提供的映射超类扩展时,不要重新定义其他字段的映射,因为这些映射是由 Bundle 提供的。
在以下章节中,您将看到根据您如何存储帖子(Doctrine ORM)您的 Post
类应该如何看起来的一些示例。
注意
文档使用了名为 ContentBundle
的 Bundle。然而,您当然可以将您的帖子类放置在您想要的任何 Bundle 中。
注意!
如果您在 Post 类中重写了 __construct() 方法,请确保调用 parent::__construct(),因为基础 Post 类依赖于它来初始化一些字段。
Doctrine ORM Post 类
如果您通过 Doctrine ORM 持久化帖子,那么您的 Post
类应该位于 Bundle 的 Entity 命名空间中,并且如下所示
*您可以在此类中添加所有与其他实体之间的关系
<?php // src/PN/Bundle/ContentBundle/Entity/Post.php namespace PN\Bundle\ContentBundle\Entity; use Doctrine\ORM\Mapping as ORM; // DON'T forget the following use statement!!! use PN\ContentBundle\Entity\Post as BasePost; use PN\ContentBundle\Model\PostTrait; /** * Post * @ORM\Table(name="post") * @ORM\Entity(repositoryClass="PN\Bundle\ContentBundle\Repository\PostRepository") */ class Post extends BasePost { use PostTrait; public function __construct() { parent::__construct(); // your own logic } }
步骤 4: 创建您的 PostRepository 类
您可以使用此 Repository
添加任何自定义方法
<?php // src/PN/Bundle/ContentBundle/Repository/PostRepository.php namespace PN\Bundle\ContentBundle\Repository; use PN\ContentBundle\Repository\PostRepository as BasePostRepository; class PostRepository extends BasePostRepository { }
步骤 5: 配置 PNContentBundle
根据您使用的数据存储类型,将以下配置添加到您的 config.yml 文件中。
# app/config/config.yml
doctrine:
orm:
# search for the "ResolveTargetEntityListener" class for an article about this
resolve_target_entities:
PN\MediaBundle\Entity\Image: PN\Bundle\MediaBundle\Entity\Image
PN\MediaBundle\Entity\Document: PN\Bundle\MediaBundle\Entity\Document
pn_content:
# The fully qualified class name (FQCN) of the Post class which you created in Step 3.
post_class: PN\Bundle\ContentBundle\Entity\Post
步骤 6: 导入 PNContentBundle 路由文件
# app/config/routing.yml
pn_media:
resource: "@PNMediaBundle/Resources/config/routing.yml"
pn_content:
resource: "@PNContentBundle/Resources/config/routing.yml"
步骤 7: 更新您的数据库模式
现在 Bundle 已配置,您需要做的最后一件事是更新数据库模式,因为您已经添加了一个新实体,即第 3 步中创建的 Post
类。
$ php bin/console doctrine:schema:update --force
如何使用 PNContentBundle
- 使用 Doctrine ORM 中的 Post 实体
- 在表单类型中使用 Post
- 如何添加自定义字段 例如:简介、描述等 ...
- 在控制器中使用 Post
- 在详情页面(如
show.html.twig
)中使用 Post
1. 使用 Doctrine ORM 中的 Post 实体
首先,您需要在 src/PN/Bundle/ContentBundle/Entity/Post.php
中添加一个关系,以便在需要使用 Post 类的实体之间建立联系 例如:Blogger、Product 等 ... 示例实体
Post.php
<?php // src/PN/Bundle/ContentBundle/Entity/Post.php namespace PN\Bundle\ContentBundle\Entity; use Doctrine\ORM\Mapping as ORM; use PN\ContentBundle\Entity\Post as BasePost; use PN\ContentBundle\Model\PostTrait; /** * Post * @ORM\Table(name="post") * @ORM\Entity(repositoryClass="PN\Bundle\CMSBundle\Repository\PostRepository") */ class Post extends BasePost { use PostTrait; // Add here your own relations /** * @ORM\OneToOne(targetEntity="\PN\Bundle\CMSBundle\Entity\DynamicPage", mappedBy="post") */ protected $dynamicPage; public function __construct() { parent::__construct(); // your own logic }
DynamicPage.php
<?php namespace PN\Bundle\CMSBundle\Entity; use Doctrine\ORM\Mapping as ORM; use PN\ServiceBundle\Model\DateTimeTrait; /** * DynamicPage * * @ORM\HasLifecycleCallbacks * @ORM\Table(name="dynamic_page") * @ORM\Entity(repositoryClass="PN\Bundle\CMSBundle\Repository\DynamicPageRepository") */ class DynamicPage { use DateTimeTrait; .... /** * @ORM\OneToOne(targetEntity="PN\Bundle\ContentBundle\Entity\Post", inversedBy="dynamicPage", cascade={"persist", "remove" }) */ protected $post; .... }
2. 在表单类型中使用 Post
您需要将 Post 类型添加到任何表单类型中,以便使用此神奇的工具
DynamicPageType.php
<?php namespace PN\Bundle\CMSBundle\Form; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; // DON'T forget the following use statement!!! use PN\ContentBundle\Form\PostType; class DynamicPageType extends AbstractType { /** * {@inheritdoc} */ public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('post', PostType::class) ...... ; } ..... }
3. 如何添加自定义字段
如果您需要为任何表单类型添加自定义字段
例如,在 DyncamicPageType.php 中添加一个 shortDescription
字段
<?php namespace PN\Bundle\CMSBundle\Form; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use PN\ContentBundle\Form\PostType; // DON'T forget the following use statement!!! use PN\ContentBundle\Form\Model\PostTypeModel; class DynamicPageType extends AbstractType { /** * {@inheritdoc} */ public function buildForm(FormBuilderInterface $builder, array $options) { $postTypeModel = new PostTypeModel(); $postTypeModel->add("description", "descriptionsss"); $postTypeModel->add("brief", "Brief"); /** documentation * @param string $name field_name (must not contain any spaces or special characters) * @param string $label field_label * @param array $options field_options */ $postTypeModel->add({field_name}, {field_label}, {field_options}); $builder ->add('post', PostType::class, [ // DON'T forget the following statement!!! "attributes" => $postTypeModel ]) ...... ; } ..... }
报告问题或功能请求
问题和功能请求在 Github 问题追踪器 中跟踪。
在报告错误时,最好在基于 Symfony 标准版 的基本项目中重现它,以便组件的开发者可以通过简单地克隆项目并遵循一些步骤来重现问题。