blok/laravel-cms

一个针对 Laravel 的 CMS 意见数据层,用于处理类似于 Strapi、Wordpress 等的无头 CMS。

1.3.7 2022-09-16 10:43 UTC

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

一个 CMS 仓库,用于将您的无头 CMS 连接到 Laravel 应用程序。

它依赖于我们发现在所有 Web 应用项目中都很容易找到的意见设计模式。通过采用务实的方法,不必重新发明轮子,您可以提升您的项目。

安装

您可以通过 composer 安装此软件包

composer require blok/laravel-cms

您可以使用以下命令发布和运行迁移

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

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

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

然后您可以通过查看 /config/laravel-cms.php 配置来覆盖它

使用 Strapi CMS(默认)安装

您必须从这个模板安装一个有效的 Strapi CMS 并遵循说明

https://github.com/uptoolkit/upcms-strapi.

然后一旦您生成了一个有效的 Token 并有了有效的 URL。

在您的 .env 中添加配置

CMS_STRAPI_URL=$yourURL$
CMS_STRAPI_TOKEN=$yourToken$

使用 Wordpress CMS 安装

您必须安装一个有效的 Wordpress CMS 以及所需的插件,并遵循以下说明

https://github.com/uptoolkit/upcms-wordpress.

您还必须安装此软件包

composer require cherrypulp/laravel-wordpress

:warning: 您必须拥有有效的 ACF Pro 和 WPML Pro 许可证才能使其正常工作 :warning: 您还必须安装 ACF to Rest Api 以将 acf 字段添加到您的 Rest API 中

然后一旦您生成了一个有效的 Token 并有了有效的 URL。

在您的 .env 中添加配置

CMS_CONNECTION=wordpress
CMS_WORDPRESS_URL=$yourURL$
CMS_WORDPRESS_TOKEN=$yourToken$

用法

一旦您设置了项目,您可以通过这些辅助方法简单访问您的 CMS 数据

cms()->pageBySlug("home");
cms()->page(1);
cms()->pages($PARAMS_OF_THE_API);

/**
* Exemple in Strapi to get localized 
 * @see https://docs.strapi.io/developer-docs/latest/developer-resources/database-apis-reference/rest-api.html
 */
cms()->pages(['locale' => 'fr', 'offset' => 20, 'limit' => 10]);

/**
* Exemple in Wordpress to get localized 
 * @see https://developer.wordpress.org/rest-api/using-the-rest-api/pagination/
 * @see https://wpml.org/documentation/related-projects/woocommerce-multilingual/using-wordpress-rest-api-woocommerce-multilingual/
 */
cms()->pages(['lang' => 'fr', 'per_page' => 10, 'page' => 2]);

cms()->postBySlug("article", 'fr');
cms()->post(1);
cms()->posts($PARAMS_OF_THE_API);

cms()->emailBySlug("welcome", "en");

cms()->notificationBySlug("welcome");
cms()->settings();
cms()->setting("name");

cms()->user();
cms()->userByEmail("john@doe.com");

cms()->menus();
cms()->menu("primary_navigation");
cms()->menuBySlug("primary_navigation");


# Exemple using all the available params from Wordpress
# @see https://developer.wordpress.org/rest-api/reference/
cms()->query('posts', ['slug' => 'welcome'], ['first' => true, 'transform' => function($data, $params, $entity){
    return $data;
}, 'namespace' => 'wp/v2']);

# Exemple using all the available params from Strapi
# @see https://docs.strapi.io/developer-docs/latest/developer-resources/database-apis-reference/rest-api.html
cms()->query('posts', ['slug' => 'welcome'], ['first' => true, 'transform' => function($data, $params, $entity){
    return $data;
}]);

如何直接访问驱动程序的 API?

开箱即用,Wordpress 或 Strapi ServiceProvider 会添加一个 http 宏 https://laravel.net.cn/docs/9.x/http-client#macros,因此您可以使用 Laravel 的 Http 客户端直接请求 API。

访问 Strapi 的完整 API

https://docs.strapi.io/developer-docs/latest/developer-resources/database-apis-reference/rest-api.html

Http::strapi()->get('xxxx', ['your params']);

访问 Wordpress 的完整 API

https://developer.wordpress.org/rest-api/reference/

Http::wordpress()->get('wp/v2/xxxx', ['your params']); 

如何创建自定义适配器?

如果您有自己的 CMS 或其他无头 CMS。

您可以通过复制粘贴 src/Repositories 文件夹中的提供者之一,并在配置文件夹中覆盖它来轻松创建自己的适配器。

至少您必须定义一个 Service Provider 和所需的最低实体存储库(请参阅配置以获取规范。)

如何覆盖或处理实体的行为?

在您的 AppServiceProvider 中,您可以重新绑定或覆盖 config/laravel-cms.php 中的任何实体的行为。

app()->bind('cms.post', function(){ return new CustomRepository();});

测试

composer test

变更日志

有关最近更改的更多信息,请参阅 CHANGELOG

贡献

有关详细信息,请参阅 CONTRIBUTING

安全漏洞

请审查 我们的安全策略 了解如何报告安全漏洞。

致谢

许可协议

MIT 许可协议 (MIT)。请参阅 许可文件 了解更多信息。