protechstudio/laravel-prestashop-webservice

Laravel 5 对 Prestashop Web Service 库的封装

3.5.0 2019-05-19 20:59 UTC

This package is auto-updated.

Last update: 2024-08-29 03:52:52 UTC


README

Laravel 5 对 Prestashop Web Service 库的封装

安装

使用以下命令通过 composer 安装此包

composer require protechstudio/laravel-prestashop-webservice

更新 composer 后,将服务提供者添加到 config/app.php 文件中的 providers 数组

Protechstudio\PrestashopWebService\PrestashopWebServiceProvider::class,

您还可以在 config/app.php 文件的 aliases 数组中添加 Facade

'Prestashop' => Protechstudio\PrestashopWebService\PrestashopWebServiceFacade::class,

最后,使用 artisan 命令发布配置文件

php artisan vendor:publish --provider="Protechstudio\PrestashopWebService\PrestashopWebServiceProvider"

配置

打开已发布的配置文件 config/prestashop-webservice.php

return [
    'url' => 'http://domain.com',
    'token' => '',
    'debug' => env('APP_DEBUG', false)
];

然后,在 url 字段中填写目标 Prestashop 安装的 根 URL,并在 token 字段中填写从 Prestashop 控制面板 Web Service 选项卡中获得的 API 令牌。如果 debug 设置为 true,Prestashop 将在响应 API 请求时返回调试信息。

使用方法

您可以使用两种方式使用 Prestashop Web Service 封装器

使用依赖项或方法注入

...
use Protechstudio\PrestashopWebService\PrestashopWebService;

class FooController extends Controller
{
    private $prestashop;
    
    public function __construct(PrestashopWebService $prestashop)
    {
        $this->prestashop = $prestashop;
    }
    
    public function bar()
    {
        $opt['resource'] = 'customers';
        $xml=$this->prestashop->get($opt);
    }
}

使用 Facade

...
use Prestashop;

...

public function bar()
{   
    $opt['resource'] = 'customers';
    $xml=Prestashop::get($opt);
}

Prestashop 基础库使用

您可以在 Prestashop 文档 中找到有关 Prestashop Web Service 库的完整文档和教程。

辅助方法

我添加了一些辅助方法以减少开发时间

检索资源模式并填充数据

您可以通过调用 getSchema() 方法检索请求的资源模式。然后,您可以使用 fillSchema() 方法用数据关联数组填充该模式。

$xmlSchema=Prestashop::getSchema('categories'); //returns a SimpleXMLElement instance with the desired schema

$data=[
    'name'=>'Clothes',
    'link_rewrite'=>'clothes',
    'active'=>true
];

$postXml=Prestashop::fillSchema($xmlSchema,$data); 

//The xml is now ready for being sent back to the web service to create a new category

$response=Prestashop::add(['resource'=>'categories','postXml'=>$postXml->asXml()]);

保留未填充节点以避免删除

fillSchema 方法的默认行为是删除未填充的节点。如果您想保留这些节点(典型的更新情况),请将第三个参数设置为 false

$putXml=Prestashop::fillSchema($xmlSchema,$data,false); 

删除特定节点

在保留未填充节点以避免删除时,您可以指定要删除的某些节点作为第四个参数(这可能在更新具有一些只读节点的资源时很有用,这些节点会触发错误 400)

$putXml=Prestashop::fillSchema($xmlSchema,$data,false,['manufacturer_name','quantity']);
//manufacturer_name and quantity only will be removed from the XML

处理语言值

如果节点有语言子节点,如果您的商店只安装了一种语言,则可以使用简单字符串作为值。

/*
    xml node with one language child example
    ...
    <name>
    <language id="1"/>
    </name>
    ...
*/
$data= ['name'=>'Clothes'];

如果您的商店安装了多个语言,则可以将节点值作为数组传递,其中键是语言 ID。

/*
    xml node with n language children example 
    ...
    <name>
    <language id="1"/>
    <language id="2"/>
    </name>
    ... 
*/
$data= [
    'name'=>[
        1 => 'Clothes',
        2 => 'Abbigliamento'
    ]
];

请注意,如果您不提供按语言 ID 键的值数组,则所有语言值都将具有相同的值。

处理具有多个兄弟的关联

假设您有一个具有多个关联的节点,例如产品的类别关联,如下所示的产品模式摘录

...
<associations>
<categories>
<category>
<id/>
</category>
</categories>
<product_features>
<product_feature>
<id/>
<id_feature_value/>
</product_feature>
</product_features>
...

您可以使用以下方式为 fillSchema 方法准备数组数据映射

$data => [
    ...
    'associations' => [
        'categories' =>[
            [ 'category' => ['id' => 4] ],
            [ 'category' => ['id' => 5] ],
            [ 'category' => ['id' => 11] ],
        ],
        'product_features' => [
            [
                'product_feature' => [
                    'id' => 5,
                    'id_feature_value' => 94
                ]
            ],
            [
                'product_feature' => [
                    'id' => 1,
                    'id_feature_value' => 2
                ]
            ]
        ]
    ]
]

结果将像预期的那样

...
<associations>
    <categories>
        <category>
            <id>4</id>
        </category>
        <category>
            <id>5</id>
        </category>
        <category>
            <id>11</id>
        </category>
    </categories>
    <product_features>
        <product_feature>
            <id>5</id>
            <id_feature_value>94</id_feature_value>
        </product_feature>
        <product_feature>
            <id>1</id>
            <id_feature_value>2</id_feature_value>
        </product_feature>
    </product_features>
...