主题支持从种子脚本导入示例内容,包括数据库内容和 Tailor 蓝图。主题内部一个名为 seeds 的特定文件夹与目录结构一起用于提供内容。
theme:seed Artisan 命令用于填充主题。
php artisan theme:seed <theme name>您也可以使用 --root 选项来指示命令将蓝图导入到根目录而不是嵌套目录中。
php artisan theme:seed <theme name> --root:::tip
你也可以通过导航到管理面板来初始化主题 设置 → 前端主题 → 管理 → 初始化内容
:::
下面您可以看到一个示例种子目录结构。blueprints 目录包含主题使用的任何蓝图模板,这些模板会自动导入到 app/blueprints 目录并在其中创建一个名为 mywebsite 的嵌套目录。data.yaml 文件包含如何将内容导入数据库的说明。
├── themes
| └── mywebsite
| └── seeds ← 主题种子目录
| ├── blueprints
| | └── post.yaml ← 蓝图文件
| ├── lang
| | └── en.json ← 语言文件
| ├── media
| | └── banner.jpg ← 媒体文件
| ├── data
| | ├── blog-posts.json ← 数据文件
| | └── media-files.json
| └── data.yaml ← 种子脚本
作为一项可选功能,可以通过将 JSON 语言文件 放置在 lang 目录中,将语言导入到 app/lang 目录。这使得翻译蓝图中的标签和其他描述成为可能。如果语言文件已存在于应用程序语言目录中,则语言字符串将被合并。
该 data.yaml 文件包含一种特定的格式,用于将内容导入数据库。在下面的示例中,两组数据被导入数据库,用于 Tailor 条目内容。
-
name: Blog Post Data
class: Tailor\Models\RecordImport
file: seeds/data/blog-posts.json
attributes:
file_format: json
blueprint_uuid: edcd102e-0525-4e4d-b07e-633ae6c18db6
-
name: Media File Data
class: Media\Models\MediaLibraryItemImport
file: seeds/data/media-files.json
attributes:
file_format: jsonYAML 文件应该定义一个数组,其中数组中的每个项支持以下属性。
| Property | Description |
|---|---|
| name | gives the import step a name to display to the user. |
| class | refers to a model that extends the interface of Backend\Models\ImportModel. |
| file | refers to the JSON data file that contains the content to import. |
| attributes | a list of attributes to set on the Import Model before importing. |
使用 Tailor\Models\RecordImport 类将 Tailor 蓝图导入到应用程序目录。以下是一个 JSON 文件示例,可用于导入博客分类。JSON 数组中的每个项目都会在数据库中生成一条导入记录,并带有所提供的属性。提供一个 id 属性允许记录在多次导入之间进行关联。
[
{
"id": 1,
"title": "Announcements",
"slug": "announcements"
},
{
"id": 2,
"title": "News",
"slug": "news"
}
]使用 Media\Models\MediaLibraryItemImport 将图片导入媒体目录。以下 JSON 是一个将文件导入媒体库的示例。rootPath 属性定义了媒体库的前缀,它可以设置为空字符串以导入根目录中的所有内容。type 属性指定 file 或 folder 用于导入各自的类型,其中 path 是目标,source 是源文件,在主题目录的上下文中查找。
[
{
"type": "folder",
"path": "my-theme/announcements",
"source": "seeds/media/announcements"
},
{
"type": "folder",
"path": "my-theme/news/2025",
"source": "seeds/media/news"
},
{
"type": "file",
"path": "my-theme/banner.jpg",
"source": "seeds/media/banner.jpg"
}
]由于蓝图不依赖于任何特定的文件或目录结构,它们可以自由移动。
导入蓝图时,只需将蓝图文件放置到 seeds/blueprints 目录下。它不使用任何配置,在播种时,所有蓝图都只是复制到 app/blueprints 目录下。在其中会创建一个新目录,该目录与主题同名。蓝图被放置在这个新目录中。
:::tip
蓝图并非总是需要通过数据填充导入。它们可以改为包含在主题的**/blueprints**目录中。参见入门文章了解更多信息。
:::