定制 netgen/layouts-contentful 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

netgen/layouts-contentful

Composer 安装命令:

composer require netgen/layouts-contentful

包简介

Netgen Layouts & Contentful integration

README 文档

README

Installation instructions

Installing Netgen Layouts

Follow the instructions in Netgen Layouts documentation to install Netgen Layouts.

Use Composer

After completing standard Netgen Layouts installation, run the following command to install Contentful integration:

composer require netgen/layouts-contentful

Symfony Flex will automatically enable the bundle and import the routes.

Configure Contentful bundle

To work with Contentful resources, you need to configure a client for every space you wish to use. For every client, you need to specify the space ID and its token. You can get space IDs and tokens from the APIs section from your Contentful instance at app.contentful.com

# config/packages/contentful.yaml
contentful:
    delivery:
        main:
            token: your_space_token
            space: your_space_identifier

For more information, see Contentful bundle official repo on Github.

Configure the CMF Routing component

The integration uses CMF routing component and its dynamic router to match routes and generate URLs to resources from Contentful. You need to enable the dynamic router in your configuration:

# config/packages/cmf_routing.yaml
cmf_routing:
    chain:
        routers_by_id:
            router.default: 200
            cmf_routing.dynamic_router: 100
    dynamic:
        default_controller: netgen_layouts.contentful.controller.view
        persistence:
            orm:
                enabled: true

For more information, see CMF Routing docs on symfony.com.

Import the schema

Import the routing and local content schema to the database.

php bin/console doctrine:schema:update

Note: Use the command with --dump-sql first to check that it will do only modifications related to this bundle, and then use it with parameter --force to do the actual changes.

Configure authentication for Netgen Layouts

It is highly recommended to secure the Netgen Layouts interface and allow only authenticated users in. For fresh Symfony installations the simplest way is to define an admin user in memory with the ROLE_NGLAYOUTS_ADMIN role and enable the HTTP basic auth firewall.

# config/packages/security.yaml
security:
    providers:
        users_in_memory:
            memory:
                users:
                    admin:
                        password: admin
                        roles: [ROLE_NGLAYOUTS_ADMIN]

    password_hashers:
        Symfony\Component\Security\Core\User\InMemoryUser: plaintext

    firewalls:
        main:
            provider: users_in_memory
            http_basic: ~

    access_control:
        - { path: ^/nglayouts/(api|(dev/)?app|admin), roles: ROLE_NGLAYOUTS_ADMIN }
        - { path: ^/cb, roles: ROLE_NGLAYOUTS_ADMIN }

Verifying that Netgen Layouts works

Start the Symfony CLI web server with:

symfony server:start

Open https://127.0.0.1:8000/nglayouts/admin/layouts, give the admin credentials and you should be able to create new layouts.

Optional: Run the sync command

To warmup the caching for spaces and content types as well as syncing Contentful entries locally run this command:

php bin/console contentful:sync

There is a limit on 100 entries in one run, so if there are more than 100 entries you can run the command multiple times.

Optional: Configure webhook

To make local cache refresh when an entry or a content type changes in Contentful add the webhook configuration.

Go to Contentful space settings and create a webhook with:

  • the full URL for webhook (https://your.domain/webhook)
  • basic auth credentials if necessary
  • additional X-Space-Id header with the related value
  • checked Published, Unpublished and Delete events for Content types
  • checked Published, Unpublished and Delete events for entries

Optional: Implement custom sluggers

This bundle offer the possibility to implement custom sluggers to generate URLs for full content based pages. Out of the box there are 2 sluggers implemented:

  • simple slugger - takes the name of the Contentful entry and makes the slug. This one is used by default
  • with_space slugger - adds the space name before the entry name so the URL will have the format /[space_name_slug]/[entry_name_slug]

To implement a custom slugger you need to implement the EntrySluggerInterface. A FilterSlugTrait is provided so you can filter the slug as required by Contentful.

<?php

declare(strict_types=1);

namespace App\Routing\EntrySlugger;

use Netgen\Layouts\Contentful\Attribute\AsEntrySlugger;
use Netgen\Layouts\Contentful\Routing\EntrySlugger\FilterSlugTrait;
use Netgen\Layouts\Contentful\Routing\EntrySluggerInterface;

#[AsEntrySlugger('with_my_prefix')]
final class WithMyPrefix implements EntrySluggerInterface
{
    use FilterSlugTrait;

    public function getSlug(ContentfulEntry $contentfulEntry): string
    {
        return '/my_prefix/' . $this->filterSlug($contentfulEntry->name);
    }
}

filterSlug() method from the trait creates an URL friendly string from any given string.

Finally, you need to configure your app to use your slugger. You can declare it as a default one and declare it for each content type using the content type ID:

# config/packages/netgen_layouts_contentful.yaml
netgen_layouts_contentful:
    entry_slug_type:
        default: with_my_prefix
        content_type:
            # Category content type
            5KMiN6YPvi42icqAUQMCQe: simple
            # Post content type
            2wKn6yEnZewu2SCCkus4as: with_my_prefix

And that should be it!

Use it

Open again https://127.0.0.1:8000/nglayouts/admin/layouts and start creating Contentful specific layouts and map them to URL targets.

netgen/layouts-contentful 适用场景与选型建议

netgen/layouts-contentful 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.05k 次下载、GitHub Stars 达 11, 最近一次更新时间为 2018 年 08 月 17 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 netgen/layouts-contentful 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 netgen/layouts-contentful 我们能提供哪些服务?
定制开发 / 二次开发

基于 netgen/layouts-contentful 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.05k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 11
  • 点击次数: 11
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 11
  • Watchers: 4
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-08-17