verinder/laravel-prestashop-webservice

Laravel 5 对 Prestashop Web 服务库的包装,支持 json

3.5.2 2018-08-22 08:17 UTC

This package is auto-updated.

Last update: 2024-09-12 05:04:15 UTC


README

Laravel 5 对 Prestashop Web 服务库的包装,在此库中,Prestashop Web 服务的响应可能包含 json。这是对 https://github.com/Silentscripter/laravel-prestashop-webservice 的分支。作者:https://github.com/Silentscripter

安装

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

composer require verinder/laravel-prestashop-webservice

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

Verinder\PrestashopWebService\PrestashopWebServiceProvider::class,

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

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

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

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

配置

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

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

然后,在 url 字段中填充目标 Prestashop 安装的 根 URL,在 token 字段中填充从 Prestashop 控制面板 Web 服务部分获得的 API 令牌。如果 debugtrue,则 Prestashop 将在响应 API 请求时返回调试信息。

用法

您可以使用两种方式使用 Prestashop Web 服务包装器

使用依赖项或方法注入

...
use Verinder\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 服务库的完整文档和教程。

辅助方法

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

检索资源模式并填充数据以进行发布

您可以通过调用 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>
...