antonioprimera/laravel-site

使用Laravel构建网站的框架,包括可重用的视图组件。

v1.1.11 2024-09-02 18:59 UTC

This package is auto-updated.

Last update: 2024-09-02 19:00:41 UTC


README

Latest Version on Packagist

此包提供了使用Laravel和Filament作为管理面板创建可配置网站的基本构建块,您可以在其中维护网站的文本和图像。

它提供了以下模型

  • Site:一个网站是一个页面集合,包含一个设置容器,可以用于存储网站的全局设置。
  • Page:一个页面是一个部分集合
  • Section:一个部分是一个数据块集合
  • Bit:一个数据块是一个数据容器,可以用于存储网站部分的某些部分。

所有模型都有一个uid属性,用于标识和检索模型。该uid是字符串,应在模型类型内唯一。例如,为了识别模型,您将使用Site Facade或辅助程序。

//gets the 'default' site
$site = site();

//gets the 'home' page of the 'default' site
$page = page('home');

//gets the 'hero' section of the 'home' page of the 'default' site
$section = section('home:hero');

//gets the 'cta' bit of the 'hero' section of the 'home' page of the 'default' site
$bit = bit('home:hero.cta');

//if you have several sites, you can specify the site key as the first argument for all the helpers
$bit = bit('my-site/home:hero.cta');

部分和数据块模型有一个单例spatie媒体'image',因此您可以轻松地将图像与它们关联。

//setting the image of a section
section('home:hero')->setImageFromMediaCatalog('path/to/image.webp');

//retrieving the image of a section
$mediaInstance = section('home:hero')->image;

此包提供用于构建页面、部分和数据块视图组件的抽象类,可用于在视图中渲染模型。您可以通过扩展这些类来创建自己的视图组件。您可以使用以下bash命令生成新的视图组件

#generates a new Page View Component and a data migration if the -m flag is set
php artisan site:page AboutUs -m

#generates a new Section View Component
php artisan site:section Hero

#generates a new Bit View Component
php artisan site:bit Cta

此外,您还可以使用antonioprimera/laravel-site-components包来获取一些预构建的组件和 artisan 命令来创建新的视图组件。

模型使用spatie/laravel-medialibrary存储图像,并使用spatie/laravel-translatable使标题和内容可翻译。

安装

您可以通过composer安装此包

composer require antonioprimera/laravel-site

发布迁移并运行它们

php artisan vendor:publish --tag="site-migrations"
php artisan migrate

您可以使用以下命令发布配置文件

php artisan vendor:publish --tag="site-config"

这是发布配置文件的内容

return [
    /**
     * The list of locales that should be used for translations
     * Make sure to include the default and fallback locale in this list
     */
    'translations' => [
        'locales' => ['en'],
        'missing-translation' => '--',  //the string that should be displayed if a translation is missing
    ],

    /**
     * Site section view component default configuration
     */
    'sections' => [

        //section images will be resized to fit these dimensions
        'image' => [
            'max-width' => 1920,
            'max-height' => 1080,
        ],
    ],

    /**
     * Data migrations configuration
     */
    'data-migrations' => [

        //the path to the directory where site data migrations are stored, relative to the database directory
        'path' => 'site-migrations',
    ],

    'model-builders' => [
        //whether the model builders should automatically save the model after each fluent method call
        //e.g. calling $builder->withName('test') will automatically save the model if this is set to true
        'fluent-auto-save' => true,
    ],

    /**
     * Media catalog configuration
     */
    'media-catalog' => [
        //the disk where media catalog files are stored
        'disk' => 'media-catalog',
    ],

    'views' => [
        //the root path for the blade views of the site components (relative to the resources/views directory)
        'bladeRootName' => 'components',

        //the namespace of the site components
        'componentNamespace' => 'App\\View\\Components\\',
    ],

    //settings for the generator commands: site:page, site:section, site:bit
    'generator-command' => [
        'pages' => [
            'rootNamespace' => 'App\\View\\Components\\Pages',
            'classTargetFolder' => 'View/Components/Pages',         //relative to the project root
            'bladeTargetFolder' => 'components/pages',              //relative to the resources/views directory
        ],

        'sections' => [
            'rootNamespace' => 'App\\View\\Components\\Sections',
            'classTargetFolder' => 'View/Components/Sections',
            'bladeTargetFolder' => 'components/sections',
        ],

        'bits' => [
            'rootNamespace' => 'App\\View\\Components\\Bits',
            'classTargetFolder' => 'View/Components/Bits',
            'bladeTargetFolder' => 'components/bits',
        ],
    ]
];

用法

该包提供以下主要功能

  • 网站、页面、部分和数据块模型,用于存储和检索您的网站数据
  • 页面、部分和数据块的视图组件生成器,以便您轻松创建新视图组件并在视图中使用模型
  • 数据迁移,用于将网站数据(使用SiteBuilder、PageBuilder、SectionBuilder和BitBuilder类创建的网站、页面、部分和数据块)填充到数据库中
  • 网站外观和辅助函数,用于通过其uid检索模型并处理网站区域

创建新的数据迁移

这些迁移文件存储在database/site-migrations目录中,并可以与您的标准laravel迁移一起使用artisan migrate运行。

php artisan site:migration CreateHomePage

您可以使用SiteBuilderPageBuilderSectionBuilderBitBuilder类在数据迁移中创建新模型,这些类提供了一种流畅的接口来创建、更新和删除模型。

以下是一个数据迁移文件的示例,它创建了一个网站、一个页面和一个部分模型,并附加了2个数据块模型

use AntonioPrimera\Site\Database\DataMigration;
use AntonioPrimera\Site\Database\ModelBuilders\SiteBuilder;
use AntonioPrimera\Site\Database\ModelBuilders\PageBuilder;
use AntonioPrimera\Site\Database\ModelBuilders\SectionBuilder;
use AntonioPrimera\Site\Database\ModelBuilders\BitBuilder;

return new class extends DataMigration {

    public function up(): void
    {
        //if this is the first migration, you should create the site first
        SiteBuilder::create('default', 'My Personal Branding Site')
            ->withData([
                'logo' => 'path/to/logo.webp',
                'favicon' => 'path/to/favicon.webp',
                'socialMedia' => [
                    'facebook' => 'https://facebook.com/my-profile',
                    'instagram' => 'https://instagram.com/my-profile',
                    'x' => 'https://x.com/my-profile',
                ],
            ]);
        
        //create the home page
        PageBuilder::create(
            uid: 'home',
            name: 'Home Page',
            title: 'My Home Page',
            short: 'This is my presentation home page',
            route: 'home',
            menuLabel: 'Home',
            menuVisible: true,
            menuPosition: 1,
            data: [
                'seo' => [
                    'title' => 'My Home Page',
                    'description' => 'This is the description of my home page',
                    'keywords' => 'home, page, website',
                ],
            ]
        );
        
        SectionBuilder::create(page: 'home', uid: 'hero', name: 'Home Hero Section')
            ->withTitle(['en' => 'Welcome to our website', 'de' => 'Willkommen auf unserer Webseite'])
            ->withContents(['en' => 'This is the hero section of the home page', 'de' => 'Dies ist der Hero-Bereich der Startseite'])
            ->withImageFromMediaCatalog('path/to/home-hero-image.webp')
            ->createBit(
                uid: 'cta',
                title: ['en' => 'Contact us', 'de' => 'Kontaktiere uns'],
                data: ['url' => '/contact', 'icon' => 'heroicon-o-phone'],
                build: fn(BitBuilder $builder) =>
                    $builder->withImageFromMediaCatalog('path/to/cta-background-image.webp', 'cta image alt text')
            )
            ->createBit(
                uid: 'motto',
                build: fn(BitBuilder $builder) =>
                    $builder->withTitle(['en' => 'Our Motto', 'de' => 'Unser Motto'])
                        ->withContents(['en' => 'This is our motto', 'de' => 'Das ist unser Motto'])
                        ->withImageFromMediaCatalog('path/to/motto-side-image.webp', 'our motto image alt text')
            );
    }

    public function down(): void
    {
        SectionBuilder::delete('home:hero');
    }
};

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件