cherrypulp / laravel-wordpress
WordPress集成到您的Laravel项目中的包助手,使用直接数据库、GraphQL或Rest集成
Requires
- php: >=7.1.6
- blok/graphql: ^1.0
- jgrossi/corcel: ^5.0
- mcamara/laravel-localization: ^1.6
This package is not auto-updated.
Last update: 2024-09-13 06:03:39 UTC
README
Laravel辅助包,用于将WordPress集成到您的项目中。它是一个包装器,用于实现3种解决方案:使用直接数据库连接(使用Corcel)、使用RestAPI(使用WP-Json)和使用GraphQL WordPress插件。
为什么有3种解决方案而不是只使用一种?
Corcel直接访问WP数据库以执行查询,这确实提供了更好的数据库访问,但不会渲染任何本地的WordPress脚本/钩子,如Gutenberg渲染或插件过滤器 查看问题。
该策略是使用WpGraphQL API(强类型)或Wp Rest API(弱类型)来允许通过WordPress进行特殊渲染,以便您可以在Laravel项目中从这3种解决方案中受益。
安装
通过composer安装
composer require cherrypulp/laravel-wordpress
注册服务提供者
注意!如果您使用laravel>=5.5并具有包自动发现功能,则此步骤和下一个步骤是可选的。
将服务提供者添加到 config/app.php
中的 providers
部分
Cherrypulp\LaravelWordpress\ServiceProvider::class,
注册外观
在 config/app.php
中的 aliases
部分注册包外观
Cherrypulp\LaravelWordpress\Facades\LaravelWordpress::class,
发布配置文件
php artisan vendor:publish --provider="Cherrypulp\LaravelWordpress\ServiceProvider" --tag="config"
设置数据库
要使其正常工作,您必须设置数据库连接到WordPress,并添加 https://github.com/wp-graphql/wp-graphql。
然后,在您的 .env 文件中,您必须定义
WP_GRAPHQL_URL="http://xxxx/wp/graphql"
WP_API_URL="http://xxxx/wp-json/"
# Important if you want to have access to protected endpoint
WP_API_TOKEN="http://xxxx/wp-json/" #Api Token @see https://developer.wordpress.org/rest-api/using-the-rest-api/authentication/#authentication-plugins on how to generate that
WP_API_TYPE="BEARER" #The type of Authentication used @see : https://developer.wordpress.org/rest-api/using-the-rest-api/authentication/#authentication-plugins for more information (default is Bearer Oauth type)
WP_DB_HOST="xxxx"
WP_DB_NAME="xxxx"
WP_DB_USER="xxxx"
WP_DB_PASSWORD="xxxx"
使用方法
此包是Laravel Corcel和WordPress GraphQL的包装器,允许直接连接到WordPress。
请参阅文档以获取更多信息 https://github.com/corcel/corcel、https://gitlab.com/cherrypulp/libraries/blok-graphql 和 https://github.com/wp-graphql/wp-graphql。
基于GraphQL的查询
GraphQL客户端基于我们的包:https://gitlab.com/cherrypulp/libraries/blok-graphql。
您基本上可以执行这样的查询
$posts = lwp()->graphql()->query($graphqlQuery, $params);
getPostBySlug
$post = lwp()->getPostBySlug('hello-world');
根据slug返回文章
可用方法
$home = lwp()->getHome();
# Return the homepage information
$post = lwp()->getPostBySlug('hello-world');
$posts = lwp()->getHighlightedPosts();
# Get the posts marked as featured posts
$category = lwp()->getCategoryBySlug('essential');
lwp()->flush()->getHome();
# Flush all cached queries
使用REST API
您可以通过
$posts = lwp()->http()->get('wp/v2/posts');
底层只是Laravel HTTP客户端的包装:https://laravel.net.cn/docs/8.x/http-client#introduction
使用Corcel模型
Corcel模型可通过lwp()->{$modelName}变量访问。
示例:Post模型可通过lwp()->post->slug('hello-world')访问
更多信息请参阅:https://github.com/corcel/corcel
深入了解
如何覆盖GraphQL查询?
您可以通过简单地将此插件的graphql/query.ql文件夹中的内容复制粘贴到您的graphql文件夹中来覆盖默认的GraphQL查询。您还可以覆盖config/laravel-wordpress.php中的配置。
如何扩展存储库?
您可以通过更改config/laravel-wordpress来替换默认的WordpressRepository
``php
"repository" => MyOwnWordpressRepository::class,
``
然后您就可以自由地实现自己的类了,但请确保实现了WordpressRepositoryContract接口。
use Cherrypulp\LaravelWordpress\Repositories;
use Cherrypulp\LaravelWordpress\Repositories\WordpressRepositoryContract;
class MyOwnWordpressRepository extends WordpressRepository implements WordpressRepositoryContract {
}
安全
如果您发现任何与安全相关的问题,请通过电子邮件而不是使用问题跟踪器。
致谢
此包是在 cherrypulp/laravel-package-generator 的帮助下进行引导的。