yamshadow / nova-page-manager
Laravel Nova 的页面和区域管理器。
Requires
- php: >=7.1.0
- doctrine/dbal: ^2.9
- laravel/nova: ^2.9 || ^3.0
- optimistdigital/nova-locale-field: ^2.0.1
- dev-master
- 3.1.6
- 3.1.5
- 3.1.4
- 3.1.3
- 3.1.2
- 3.1.1
- 3.1.0
- 3.0.4
- 3.0.3
- 3.0.2
- 3.0.1
- 3.0.0
- 2.3.2
- 2.3.1
- 2.3.0
- 2.2.2
- 2.2.1
- 2.2.0
- 2.1.1
- 2.1.0
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.9.9
- 1.9.8
- 1.9.7
- 1.9.6
- 1.9.5
- 1.9.4
- 1.9.3
- 1.9.2
- 1.9.1
- 1.9.0
- 1.8.2
- 1.8.1
- 1.8.0
- 1.7.4
- 1.7.3
- 1.7.2
- 1.7.1
- 1.7.0
- 1.6.1
- 1.6.0
- 1.5.2
- 1.5.1
- 1.5.0
- 1.4.0
- 1.3.1
- 1.3.0
- 1.2.3
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.0
- 1.0.2
- 1.0.1
- 1.0.0
This package is auto-updated.
Last update: 2024-09-22 22:15:38 UTC
README
此 Laravel Nova 包允许您创建和管理页面和区域。该包面向无头 CMS。
要求
- Laravel Nova <= 2.0.7 || >= 2.0.10
Laravel Nova 2.0.8 和 2.0.9 对 Nova Page Manager 是破坏性的。
功能
- 页面和区域管理
- 用于页面和区域的程序化模板
- 多语言支持
- 可选的页面草稿支持
屏幕截图
安装
通过 Composer 在 Laravel Nova 项目中安装此包并运行迁移
# Install package composer require optimistdigital/nova-page-manager # Run automatically loaded migrations php artisan migrate
发布 nova-page-manager
配置文件并按您的喜好编辑它
php artisan vendor:publish --provider="OptimistDigital\NovaPageManager\ToolServiceProvider" --tag="config"
在 NovaServiceProvider
的 tools()
方法中注册工具
// in app/Providers/NovaServiceProvider.php public function tools() { return [ // ... new \OptimistDigital\NovaPageManager\NovaPageManager ]; }
用法
创建模板
可以使用以下 Artisan 命令创建模板
php artisan pagemanager:template {className}
这将询问您一些额外的详细信息,并在 App\Nova\Templates
中创建一个基本模板。
模板基有一些属性
// Define whether the template is for a page or a region // Applicable values: 'page', 'region' public static $type = 'page'; // The unique name for the page, usually similar to a slug public static $name = 'about-us'; // The package has built in SEO fields support // This boolean decides whether or not to display them public static $seo = false; // If you want to have multiple views with different // templates, you can set two templates to have the // same 'view' string and use it instead for matching public static $view = null; // Return all fields here, just as you would inside a resource public function fields(Request $request): array { return [ Text::make('Title', 'title') ]; }
注册模板
您必须将所有模板注册在 config/nova-page-manager.php
配置文件中。
// in /config/nova-page-manager.php // ... 'templates' => [ \App\Nova\Templates\HomePageTemplate::class, ], // ...
定义区域
区域可以类似于模板注册的方式进行定义。配置接受区域字典。
// in /config/nova-page-manager.php // ... 'locales' => [ 'en' => 'English', 'et' => 'Estonian', ], // OR 'locales' => function () { return Locale::all()->pluck('name', 'key'); }, // if you wish to cache the configuration, pass a reference instead: 'locales' => NovaPageManagerConfiguration::class . '::locales', // ...
启用页面草稿功能
草稿功能允许您在发布页面之前创建页面的预览。默认情况下,此功能未安装,但您可以使用以下命令安装 nova-drafts。
composer require optimistdigital/nova-drafts
修改页面路径
要为页面路径添加区域前缀或修改 Page
模型上的页面路径,请在配置中提供 page_path
的回调。
// in /config/nova-page-manager.php // ... 'page_path' => function (Page $page) { return "{$page->locale}/{$page->path}"; }, // if you wish to cache the configuration, pass a reference instead: 'page_path' => NovaPageManagerConfiguration::class . '::pageUrl', // ...
添加前端页面链接
要显示与 slug 旁边的实际页面链接,请添加或覆盖 config/nova-page-manager.php
中的 page_url
键的闭包。
// in /config/nova-page-manager.php // ... 'page_url' => function (Page $page) { return env('FRONTEND_URL') . $page->path; }, // if you wish to cache the configuration, pass a reference instead: 'page_url' => NovaPageManagerConfiguration::class . '::pageUrl', // ...
覆盖包资源
您可以通过在 nova-page-manager.php
中设置配置选项来覆盖包资源(页面和区域)。
注意:如果您在 App\Nova
命名空间下创建资源,为了避免键重复,您必须手动在 NovaServiceProvider
中注册所有其他资源。有关在 Nova 文档中注册资源的更多信息,请参阅 registering resources。
修改字段值
所有字段都注册了一个宏 resolveResponseUsing(callable $resolveResponseCallback)
,允许您在通过页面管理器 API(即 nova_get_page()
)返回之前修改字段的值。
回调的签名是:function ($value, $templateModel) { ... }
。
例如
Multiselect::make('Products')
->options(Product::all()->pluck('name', 'id'))
->resolveResponseUsing(function ($value, $templateModel) {
return Product::findMany($value);
}),
辅助函数
nova_get_pages_structure($previewToken)
辅助函数 nova_get_pages_structure($previewToken)
返回基本页面结构(slugs、模板、父子关系),您可以在前端基于这些结构构建路由。此函数不返回页面数据。预览令牌是可选的,仅在启用草稿功能时使用。默认情况下,草稿不会包含在结构中。
示例响应
[ { "locales": ["en_US", "et_EE"], "id": { "en_US": 3, "et_EE": 4 }, "name": { "en_US": "Home", "et_EE": "Kodu" }, "slug": { "en_US": "/", "et_EE": "/" }, "template": "home-page", "children": [ { "locales": ["en_US"], "id": { "en_US": 5 }, "name": { "en_US": "About" }, "slug": { "en_US": "about" }, "template": "home-page" } ] } ]
nova_get_regions()
辅助函数 nova_get_regions()
返回所有区域及其数据。
示例响应
[ { "locales": ["en_US"], "id": { "en_US": 3 }, "name": { "en_US": "Main header" }, "template": "main-header", "data": { "en_US": { "content": [ { "layout": "horizontal-text-section", "attributes": { "text": "Lorem ipsum" } } ] } } } ]
nova_get_page($pageId)
辅助函数 nova_get_page($pageId)
用于查找并返回给定 ID 的页面。
查询 ID 为 3
的页面(nova_get_page(3)
)的示例响应
{ "locale": "en_US", "id": 3, "name": "Home", "slug": "/", "data": { "banner": [], "categories_grid": [] }, "template": "home-page" }
nova_get_page_by_slug($slug, $previewToken)
辅助函数 nova_get_page_by_slug($slug, $previewToken)
用于查找并返回给定 slug 的页面。预览令牌是可选的,当启用草稿功能时,用于查询草稿页面。
查询 slug 为 /home
且预览令牌为 L1SVNKDzBNVkBq8EQSna
的页面(nova_get_page("home", "L1SVNKDzBNVkBq8EQSna")
)的示例响应
{ "locale": "en_US", "id": 3, "name": "Home", "slug": "/", "data": { "banner": [], "categories_grid": [] }, "template": "home-page", "preview_token": "L1SVNKDzBNVkBq8EQSna" }
nova_get_page_by_path($slug, $previewToken, $locale)
辅助函数 nova_get_page_by_path($slug, $previewToken, $locale)
用于查找并返回给定路径的页面及其所有父级页面。预览令牌和区域设置是可选的。预览令牌用于启用草稿功能时查询草稿页面。
查询 slug 为 /home/about
且预览令牌为 L1SVNKDzBNVkBq8EQSna
的页面(nova_get_page("home/about", "L1SVNKDzBNVkBq8EQSna")
)的示例响应
{ "locale": "en_US", "id": 2, "name": "about", "slug": "about", "parent": { "locale": "en_US", "id": 1, "name": "home", "slug": "home", "path": "/home", "parent_id": null, "data": { "banner": [], "categories_grid": [] }, "template": "home-page" }, "parent_id": 1, "template": "about-page", "preview_token": "L1SVNKDzBNVkBq8EQSna", "path": "/home/about" }
致谢
许可证
Nova 页面管理器是开源软件,采用 MIT 许可证。