ikoncept / product-manager-adapter
与产品注册API结合使用
0.1.0
2024-07-03 06:45 UTC
Requires
- php: ^8.1
- illuminate/contracts: ^9.0|^10.0|^11.0
- illuminate/support: ^9.30|^10.0|^11.0
- infab/core: ^2.2
- spatie/laravel-package-tools: ^1.13.0
Requires (Dev)
- laravel/pint: ^1.0
- nunomaduro/collision: ^6.0
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^7.0
- pestphp/pest: ^1.21
- pestphp/pest-plugin-laravel: ^1.1
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.5
README
安装
您可以通过composer安装此包
composer require ikoncept/product-manager-adapter
php artisan vendor:publish --tag="product-manager-adapter-config"
这是发布配置文件的内容
return [ 'api_key' => env('PRODUCT_MANAGER_ADAPTER_KEY'), 'endpoint' => env('PRODUCT_MANAGER_ADAPTER_ENDPOINT', 'https://products.malmsten.com') ];
使用方法
路由
此包包含一些不同的控制器方法,以便更容易与产品API交互。
注意:您需要自己注册路由
推荐公共路由
// Get a tree node by ID. Usually when retrieving navigation on the front end Route::get('/tree-nodes/{id}', [TreeNodeController::class, 'show']); // Get a tree node via slug, usefull when retrieving a tree node from an url parameter Route::get('/tree-nodes/{slug}/slug', [TreeNodeSlugsController::class, 'show']); // Get a product with a specific SKU Route::get('/products/{sku}', [ProductController::class, 'show']);
推荐私有路由
// Get a list of all available product trees Route::get('/product-trees', [ProductTreeController::class, 'index']); // Get a tree representation of a specific node Route::get('/node-trees/{id}', [NodeTreeController::class, 'index']);
指定语言
产品API支持多种语言。所需语言通过 X-LOCALE
头设置。以下为示例
const payload = { params: { include: 'content', }, headers: { 'X-LOCALE': this.$i18n.locale, }, } const { data } = await this.$axios.get('/api/products', payload)
过滤活动产品树中的产品
要防止其他树的产品包含在各种API调用中,可以使用 X-PRODUCT-TREES
头
const payload = { params: { include: 'content', }, headers: { 'X-PRODUCT-TREES': 3 }, } const { data } = await this.$axios.get('/api/products', payload)
使用搜索过滤器完整示例
const payload = { params: { include: 'content', number: 10, 'filter[nodeIds]': this.nodeRootIds.join(','), 'filter[search]': this.searchQuery, }, headers: { 'X-LOCALE': this.$i18n.locale, 'X-PRODUCT-TREES': this.$store.getters['nav/productTreeRootIds'].join(',') }, } const { data } = await this.$axios.get('/api/products', payload)
测试
composer test