ameax/content-container-core 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

ameax/content-container-core

Composer 安装命令:

composer require ameax/content-container-core

包简介

A flexible, framework-agnostic content container system for Laravel applications with support for multiple container types, layouts, scheduling, and multi-tenant context filtering.

README 文档

README

Latest Version on Packagist GitHub Tests Action Status Total Downloads

A flexible, framework-agnostic content container system for Laravel applications. This package provides the core infrastructure for building modular content management systems with support for multiple container types, layouts, scheduling, and multi-tenant context filtering.

Features

  • Polymorphic Containers: Attach containers to any Eloquent model (pages, products, categories, etc.)
  • Flexible Type System: Define custom container types with their own data structures and layouts
  • Layout Support: Multiple visual layouts per container type
  • Visibility Scheduling: Show containers based on fixed dates or recurring schedules
  • Context Filtering: Multi-tenant support with customizable context scoping
  • View Resolution: Theme-aware view resolution with fallback support
  • URL Resolution: Abstract URL resolution for internal references

Installation

Install the package via Composer:

composer require ameax/content-container-core

Publish the config file:

php artisan vendor:publish --tag="content-container-core-config"

Database Setup

This package provides migration stubs that you can use as a reference. Copy them to your project and customize as needed:

# The stubs are located in:
# vendor/ameax/content-container-core/database/migrations/create_containers_table.php.stub
# vendor/ameax/content-container-core/database/migrations/create_container_references_table.php.stub

Or create your own migrations based on the schema requirements below.

Configuration

// config/content-container-core.php
return [
    'models' => [
        'container' => \App\Models\Container::class,
        'container_reference' => \App\Models\ContainerReference::class,
    ],

    'resolvers' => [
        'view' => \App\Services\MyViewResolver::class,
        'url' => \App\Services\MyUrlResolver::class,
        'context_filter' => \App\Services\MyContextFilter::class,
    ],

    'tables' => [
        'containers' => 'containers',
        'container_references' => 'container_references',
    ],
];

Usage

1. Create Your Container Model

Extend the base container model with your project-specific implementation:

<?php

namespace App\Models;

use Ameax\ContentContainerCore\Models\BaseContainer;
use App\Enums\ContainerTypeEnum;
use App\Enums\ContainerLayoutEnum;

class Container extends BaseContainer
{
    protected $casts = [
        'type' => ContainerTypeEnum::class,
        'layout' => ContainerLayoutEnum::class,
        'display_from' => 'datetime',
        'display_to' => 'datetime',
        'data' => 'array',
        'recurring_settings' => 'array',
    ];

    public function isContainerCollection(): bool
    {
        return $this->type === ContainerTypeEnum::CONTAINER_COLLECTION;
    }

    // Add your tenant/context relationship
    public function tenant()
    {
        return $this->belongsTo(Tenant::class);
    }
}

2. Define Container Types

Implement the ContainerTypeContract with your enum:

<?php

namespace App\Enums;

use Ameax\ContentContainerCore\Contracts\ContainerTypeContract;
use Ameax\ContentContainerCore\Contracts\ContainerLayoutContract;

enum ContainerTypeEnum: string implements ContainerTypeContract
{
    case TEXT = 'text';
    case IMAGE = 'image';
    case IMAGE_TEXT = 'image_text';
    case CONTAINER_COLLECTION = 'container_collection';

    public function getValue(): string
    {
        return $this->value;
    }

    public function getDefaults(): array
    {
        return match ($this) {
            self::TEXT => ['headline' => null, 'content' => null],
            self::IMAGE => ['image' => null, 'alt' => null],
            self::IMAGE_TEXT => ['headline' => null, 'content' => null, 'image' => null],
            self::CONTAINER_COLLECTION => ['container_references' => []],
        };
    }

    public function getAvailableLayouts(): array
    {
        return match ($this) {
            self::IMAGE_TEXT => [
                ContainerLayoutEnum::DEFAULT,
                ContainerLayoutEnum::OVERLAY,
            ],
            default => [ContainerLayoutEnum::DEFAULT],
        };
    }

    public function getView(ContainerLayoutContract $layout): string
    {
        // Implement your view resolution logic
        return "containers.{$layout->getViewDirectory()}.{$this->value}";
    }

    public function supports(string $feature): bool
    {
        return match ($feature) {
            'scheduling' => true,
            'collections' => $this === self::CONTAINER_COLLECTION,
            default => false,
        };
    }

    public function getIcon(): string
    {
        return asset("images/containers/{$this->value}.png");
    }

    public function getLabel(): ?string
    {
        return ucfirst(str_replace('_', ' ', $this->value));
    }
}

3. Add Containers to Your Models

Use the HasContainers trait on models that should have containers:

<?php

namespace App\Models;

use Ameax\ContentContainerCore\Traits\HasContainers;
use Illuminate\Database\Eloquent\Model;

class Page extends Model
{
    use HasContainers;

    protected function getContainerClass(): string
    {
        return \App\Models\Container::class;
    }
}

Then use containers in your views:

// Get visible containers (respects scheduling and context filtering)
$containers = $page->visibleContainers();

// Get containers for a specific slot
$headerContainers = $page->containersForSlot('header');

// Get main content containers (no slot)
$mainContainers = $page->mainContainers();

4. Implement Context Filtering (Optional)

For multi-tenant applications, implement the ContextFilterContract:

<?php

namespace App\Services;

use Ameax\ContentContainerCore\Contracts\ContextFilterContract;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\Relation;

class TenantContextFilter implements ContextFilterContract
{
    public function getContextColumn(): string
    {
        return 'tenant_id';
    }

    public function getContextRelationship(): ?string
    {
        return 'tenant';
    }

    public function getDefaultValue(): mixed
    {
        return auth()->user()?->tenant_id;
    }

    public function applyScope(Builder|Relation $query): void
    {
        $tenantId = $this->getCurrentContextValue();

        if ($tenantId !== null) {
            $query->where('tenant_id', $tenantId);
        }
    }

    public function getCurrentContextValue(): mixed
    {
        return auth()->user()?->tenant_id;
    }

    public function isEnabled(): bool
    {
        return !app()->runningInConsole();
    }
}

5. Implement View Resolution (Optional)

For theme-aware view resolution:

<?php

namespace App\Services;

use Ameax\ContentContainerCore\Contracts\ContainerTypeContract;
use Ameax\ContentContainerCore\Contracts\ContainerLayoutContract;
use Ameax\ContentContainerCore\Contracts\ViewResolverContract;

class ThemeViewResolver implements ViewResolverContract
{
    public function resolve(ContainerTypeContract $type, ContainerLayoutContract $layout): string
    {
        $theme = config('app.theme', 'default');
        $typeView = $type->getValue();
        $layoutView = $layout->getViewDirectory();

        // Try theme-specific view first
        $view = "themes.{$theme}.containers.{$layoutView}.{$typeView}";

        if (view()->exists($view)) {
            return $view;
        }

        // Fall back to default theme
        return "themes.default.containers.{$layoutView}.{$typeView}";
    }
}

Scheduling

Containers support three scheduling modes:

None (Always Visible)

$container->schedule_type = ScheduleType::NONE;

Fixed Date Range

$container->schedule_type = ScheduleType::FIXED;
$container->display_from = now();
$container->display_to = now()->addWeek();

Recurring (Annual)

$container->schedule_type = ScheduleType::RECURRING;
$container->recurring_settings = [
    'start_month_day' => '15/12',  // December 15
    'end_month_day' => '06/01',    // January 6 (supports year-spanning)
];

Container Collections

Container collections allow you to group and reuse containers:

// Create a collection container
$collection = Container::create([
    'type' => ContainerTypeEnum::CONTAINER_COLLECTION,
    'data' => [
        'container_references' => [
            ['referenced_container_id' => 1, 'sort' => 0],
            ['referenced_container_id' => 2, 'sort' => 1],
        ],
    ],
]);

// Access referenced containers
$referencedContainers = $collection->referencedContainers;

Database Schema

containers table

Column Type Description
id bigint Primary key
containerable_type string Polymorphic type
containerable_id bigint Polymorphic ID
type string Container type identifier
layout string Layout identifier
slot string Optional slot name (e.g., 'header', 'sidebar')
label string Internal label for admin
data json Container-specific data
sort integer Sort order
is_active boolean Visibility flag
schedule_type string 'none', 'fixed', or 'recurring'
display_from timestamp Start of visibility (fixed scheduling)
display_to timestamp End of visibility (fixed scheduling)
recurring_settings json Recurring schedule config
created_at timestamp
updated_at timestamp

container_references table

Column Type Description
id bigint Primary key
collection_container_id bigint FK to containers
referenced_container_id bigint FK to containers
sort integer Sort order within collection
created_at timestamp
updated_at timestamp

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

ameax/content-container-core 适用场景与选型建议

ameax/content-container-core 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 19 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 12 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「cms」 「laravel」 「layouts」 「multi-tenant」 「scheduling」 「containers」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 ameax/content-container-core 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 19
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 36
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-12-14