plank/contentable
Composer 安装命令:
composer require plank/contentable
包简介
Create dynamic pages using polymorphic relationships and special Module classes
关键字:
README 文档
README
Laravel Contentable
This package allows for models to be built up dynamically by attaching Content to it. It's intended use is to allow for
creating a module system that plays nicely with Laravel Nova (via Repeaters, or other block editing systems) to create user
defined pages.
Considerations have been made to keep this package compatible with other packages in the Plank ecosystem such as Snapshots. It also has been architected to allow for explicit linking between modules and other entities within an application.
Table of Contents
Installation
You can install the package via composer:
composer require plank/contentable
Quick Start
Once the installation has completed, to begin using the package:
- Add the
HasContenttrait andContentableinterface to any model you'd like to attach content too. - Add the
CanRendertrait andRenderableinterface to any models that will act as "Modules". - Implement the missing
Renderableinterface methods, specifically therenderHtml()method. Optionally add a$renderableFieldsclass property, listing all fields that should be accessed by the module.
Configuration
The package's configuration file is located at config/contentable.php. If you did not publish the config file during installation, you can publish the configuration file using the following command:
php artisan vendor:publish --tag=contentable-config
# Be sure to run any associated migrations
php artisan migrate
Usage
Building a Module system
Contentable's main purpose is to ease the building of a page builder style experience while maintaining explicit relationships
between Renderable models and any other arbitrary models within the application. This enables easily building things like
"Callout" modules, that can link to a concrete record in the database.
Define Modules
To take advantage of the above approach, any modules that are distinct from each other must be defined as their own models. Eg, you might define a simple "Text" module (that is, a section on a page that displays a title and body text) like so:
<?php namespace App\Models; use App\Models\Contracts\Renderable; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Plank\Contentable\Concerns\CanRender; class TextModule extends Model implements Renderable { use CanRender; use HasFactory; protected $guarded = ['id']; protected array $renderableFields = ['title', 'body']; public function renderHtml(): string { return "<h2>{$this->title}</h2>" + "<p>{$this->body}</p>"; } }
Of course, associated migrations, factories, etc... would need to be generated as well.
Define Contentables
Once modules have been created, though, they can then be attached to some Contentable, such as a Page model
eg:
<?php namespace App\Models; use App\Models\Concerns\HasContent; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Plank\Contentable\Contracts\Contentable; class Page extends Model implements Contentable { use HasContent; use HasFactory; /** * The attributes that aren't mass assignable. * * @var array */ protected $guarded = ['id']; /** * Get the route key for the model. */ public function getRouteKeyName(): string { return 'slug'; } }
Such a page can now have any number of modules attached to it, and rendered. This allows most Laravel oriented content management
systems to simply relate a module to a Page via a (polymorphic) relationship field.
You can even get creative and use repeater style fields to build up a page's content in a more editorial fashion!
Layouts
Layouts enable the user to control the "window dressing" around the modules that are laid out on a page. Effectively, a
layout represents the template used by a Contentable model (or any other model for that matter).
Layouts natively support Blade templates and Inertia pages.
Layouts are created automatically when Blade / JS files are created in the appropriate locations.
By default, these locations are resources/views/layouts for Blade templates or resources/js/Pages/Layouts for Inertia based systems.
Calling the function php artisan contentable:sync will scan these directories
Layoutables
Typically, any model with that fulfils the Conentable contract will also want to be Layoutable. This allows
users to have full control over the display modes on a page. The above Page model can be extended as so to add Layout functionality:
use Illuminate\Database\Eloquent\Model; use App\Models\Concerns\HasLayouts; use Plank\Contentable\Contracts\Contentable; use Plank\Contentable\Contracts\Layoutable; class Page extends Model implements Contentable, Layoutable { // ... use HasLayouts; /** * (Optional: Allows for layouts to be model specific) * Restrict the Page layouts to ones strictly in the Pages folder */ protected static $globalLayouts = false; }
Contentable exposes a function that make it easy to have a particular instance of a model use its set layout. Simply call ->layout() on it, and pass that to the chosen render function.
eg:
public function show(Page $page): \Illuminate\View\View { return view($page->layout())->with(compact('page')); }
Contributing
Please see CONTRIBUTING for details.
Credits
License
The MIT License (MIT). Please see License File for more information.
Security Vulnerabilities
If you discover a security vulnerability within siren, please send an e-mail to security@plankdesign.com. All security vulnerabilities will be promptly addressed.
plank/contentable 适用场景与选型建议
plank/contentable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6.22k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2023 年 10 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「plank」 「contentable」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 plank/contentable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 plank/contentable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 plank/contentable 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A targeted version of the Laravel Frameworks withoutEvents method.
A package for keeping a history of your models' revisions and accessing your data as it was at an older date.
Retrieve all defined Models available in the Application or Vendor namespaces
A Laravel package to emit events based on the schema changes taking place during migration.
Snapshot Laravel Models
统计信息
- 总下载量: 6.22k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 11
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-10-30
