laradium/laradium-content

v2.0.4 2020-04-16 15:05 UTC

README

安装

本地使用

  1. composer.json 文件中将此添加到您的项目仓库列表
"repositories": [
    {
      "type": "path",
      "url": "../packages/laradium"
    },
    {
      "type": "path",
      "url": "../packages/laradium-content"
    }
  ]

目录结构应如下所示

-Project
-packages
--laradium
--laradium-content

全局使用

"repositories": [
        {
            "type": "git",
            "url": "https://github.com/laradium/laradium.git"
        },
        {
            "type": "git",
            "url": "https://github.com/laradium/laradium-content.git"
        }
    ]
  1. composer require laradium/laradium-content dev-master
  2. php artisan vendor:publish --tag=laradium-content
  3. 使用您的偏好配置 config/laradium.php 文件
  4. 将 widgetConstructor 字段添加到您的 laradium.php 字段列表
'widgetConstructor' => \Laradium\Laradium\Content\Base\Fields\WidgetConstructor::class,

用法

默认情况下,提供了一个带有小部件构造器的主频道,允许您根据需要创建可排序的小部件块。

创建新频道

  1. 运行此命令(它将自动为您创建模型。您可以通过传递 --t 参数来创建翻译模型
php artisan laradium:channel Blog

它将在 App\Laradium\Channels 下创建新的频道

它应如下所示

<?php

namespace App\Laradium\Channels;

use App\Models\Channels\Blog;
use Laradium\Laradium\Base\FieldSet;
use Laradium\Laradium\Content\Models\Page;

Class BlogChannel
{

    /**
     * @param FieldSet $set
     */
    public function fields(FieldSet $set)
    {
        $set->morphsTo(Blog::class, Page::class)->fields(function (FieldSet $set) {
            $set->text('author');
            $set->wysiwyg('content');
        })->morphName('content');

        $set->widgetConstructor();
    }
}

您需要为博客频道创建模型,您可以在其中指定所有需要的列,并通过使用 morphsTo 字段类型将它们添加到频道中

创建小部件

  1. 运行此命令 php artisan laradium:widget它将自动为您创建模型。您可以通过传递 --t 参数来创建翻译模型

它将在 App\Laradium\Widgets 下创建一个小部件

<?php

namespace App\Laradium\Widgets;

use App\Models\Widgets\Hiw;
use Laradium\Laradium\Base\FieldSet;
use Laradium\Laradium\Content\Base\AbstractWidget;

class HiwWidget extends AbstractWidget
{

    /**
     * @var string
     */
    protected $model = Hiw::class;

    /**
     * @var string
     */
    protected $view = 'widgets.HiwWidget';

    /**
     * @param FieldSet $set
     * @return mixed|void
     */
    public function fields(FieldSet $set)
    {
        $set->text('title')->translatable();
        $set->text('description')->translatable();
        $set->hasMany('items')->fields(function (FieldSet $set) {
            $set->text('title')->translatable();
            $set->text('description')->translatable();
        })->sortable('sequence_no');
    }
}

您可以在 fields 方法下指定您的字段。

页面解析器

要实际返回创建的页面,您需要在您的路由文件末尾或 RouteServiceProvider.php 文件中添加页面解析器路由。

为此,您可以使用辅助方法 content()->pageRoute(),或者您可以创建自己的路由,但由于解析器路由是可配置的,因此通常不需要这样做。

配置文件提供了一些选项,以便在需要自定义时更改页面解析器的行为。

  • middlewares - 更改解析器路由将使用哪些中间件
  • custom_uri - 提供了一个选项,用于定义一个返回自定义 uri 的函数名
  • prepend_locale - 向 uri 添加一个 iso 代码。因此,url https://example.com/my-cool-page 将变为 https://example.com/en/my-cool-page
  • uses - 定义将用于实际解析路由的控制器和方法。