webpress/product-manager

此包最新版本(3.1.87)没有可用的许可证信息。

Webpress 产品管理器

3.1.87 2022-06-17 10:29 UTC

README

Laravel 框架中管理产品的产品管理包

安装

Composer

要将此包包含到您的项目中,请运行以下命令。

composer require vicoders/product_management

服务提供者

在您的 config/app.php 文件中,将以下服务提供者添加到 providers 数组末尾

'providers' => [
        ...
    VCComponent\Laravel\Product\Providers\ProductServiceProvider::class,
    VCComponent\Laravel\Product\Providers\ProductRouteProvider::class,
],

配置和迁移

运行以下命令以发布配置和迁移文件。

php artisan vendor:publish --provider="VCComponent\Laravel\Product\Providers\ProductServiceProvider"
php artisan vendor:publish --provider="Dingo\Api\Provider\LaravelServiceProvider"
php artisan vendor:publish --provider "Prettus\Repository\Providers\RepositoryServiceProvider"

创建表

php artisan migrate

环境

.env 文件中,我们需要一些配置。

API_PREFIX=api
API_VERSION=v1
API_NAME="Your API Name"
API_DEBUG=false

配置

URL 命名空间

为了避免与您的应用程序的 api 端点重复,该包为其路由有一个默认命名空间,为 product-management。例如

{{url}}/api/product-management/admin/product

您可以通过修改 .env 文件中的 PRODUCT_COMPONENT_NAMESPACE 变量来将包的 URL 命名空间修改为您想要的任何内容。

PRODUCT_COMPONENT_NAMESPACE="your-namespace"

模型和转换器

您可以通过修改配置文件 config\product.php 来使用自己的模型和转换器类

'models'          => [
    'product' => App\Entities\Product::class,
],

'transformers'    => [
    'product' => App\Transformers\ProductTransformer::class,
],

您的 Product 模型类必须实现 VCComponent\Laravel\Product\Contracts\ProductSchemaVCComponent\Laravel\Product\Contracts\ProductManagement

<?php

namespace App\Entities;

use Illuminate\Database\Eloquent\Model;
use Prettus\Repository\Contracts\Transformable;
use Prettus\Repository\Traits\TransformableTrait;
use VCComponent\Laravel\Product\Contracts\ProductManagement;
use VCComponent\Laravel\Product\Contracts\ProductSchema;
use VCComponent\Laravel\Product\Traits\ProductManagementTrait;
use VCComponent\Laravel\Product\Traits\ProductSchemaTrait;

class Product extends Model implements Transformable, ProductSchema, ProductManagement
{
    use TransformableTrait, ProductSchemaTrait, ProductManagementTrait;

    const STATUS_PENDING = 1;
    const STATUS_ACTIVE  = 2;

    protected $fillable = [
        'name',
        'description',
        'status',
        'slug',
        'price',
    ];
}

认证中间件

在配置文件 config\product.php 中配置认证中间件

'auth_middleware' => [
        'admin'    => [
            'middleware' => 'jwt.auth',
            'except'     => ['index'],
        ],
        'frontend' => [
            'middleware' => 'jwt.auth',
            'except'     => ['index'],
        ],
],

如果您有额外的字段,只需将 schema 添加到 Product 模型类中。

public function schema()
{
    return [
        'regular_price' => [
            'type' => 'text',
            'rule' => ['nullable'],
        ]
    ];
}

提供查询函数

存储库

查询函数列表

按字段查找

public function findProductByField($field, $value)

按条件数组查找产品

public function findByWhere(array $where, $number = 10, $order_by = 'order', $order = 'asc');

public function findByWherePaginate(array $where, $number = 10, $order_by = 'order', $order = 'asc')
// Find products by condition array withPaginate

按 id 获取产品

public function getProductByID($product_id);

按大小获取产品图片列表

public function getProductMedias($product_id, $image_dimension= '');

获取产品链接

public function getProductUrl($product_id);

获取同一分类的相关产品

public function getRelatedProducts($product_id, array $where = [], $number = 10, $order_by = 'order', $order = 'asc');

public function getRelatedProductsPaginate($product_id, array $where = [], $number = 10, $order_by = 'order', $order = 'asc');
// get related products of the same category with pagination

按分类获取产品

public function getProductsWithCategory($category_id, array $where = [], $number = 10, $order_by = 'order', $order = 'asc', $columns = ['*']);

public function getProductsWithCategoryPaginate($category_id, array $where = [], $number = 10, $order_by = 'order', $order = 'asc', $columns = ['*']);
// get products by pagination category

按关键字搜索产品

public function getSearchResult($key_word,array $list_field  = ['name'], array $where = [], $category_id = 0,$number = 10,$order_by = 'order', $order = 'asc', $columns = ['*']);

public function getSearchResultPaginate($key_word, array $list_field  = ['name'], array $where = [], $category_id = 0,$number = 10,$order_by = 'order', $order = 'asc', $columns = ['*']);
// Search product by keyword with pagination

使用

在控制器中使用 PostRepository 并添加 __construct 函数

use VCComponent\Laravel\Product\Repositories\ProductRepository;
public function __construct(ProductRepository $productRepo) 
{
    $this->productRepo = $productRepo;
}

例如

$product = $this->productRepo->findProductByField('name','product hot');
// get a product named hot product

$productWhere = $this->productRepo->findByWhere(['name'=>'product hot','status'=>1]);
// get a product named hot product and status = 1

$productWhere = $this->productRepo->findByWherePaginate(['name'=>'product hot','status'=>1]);
// get a product named hot product and status = 1 with paginate

$productById = $this->productRepo->getProductByID(1);
// get product with id = 1

$productMedia = $this->productRepo->getProductMedias(2);
// get a list of images of product with id = 2

$product = $this->productRepo->getProductUrl(1);
// get the product link with id = 1

$productsRelated = $this->productRepo->getRelatedProducts(1);
// get all products in the same category as the product with id = 1

$productsRelatedPaginate = $this->productRepo->getRelatedProductsPaginate(1);
// get all products of the same category as the product with id=1 with pagination

$productsWithCategory = $this->productRepo->getProductsWithCategory(1);
// get all products in category id = 1 

$productsWithCategoryPaginate = $this->productRepo->getProductsWithCategory(1);
// get all products of category id = 2  with pagination

$productsResult = $this->productRepo->getSearchResult('hot',['name','description']);
// get all products that contain "hot" in name or description field 

$productsResult = $this->productRepo->getSearchResult('hot',['name','description'],['status'=>1],3);
// get all product that contain "hot" in name or description field and have status = 1 field and belong to category with id = 3

$productsResult = $this->productRepo->getSearchResultPaginate('hot',['name','description'],['status'=>1],3);
// get all product that contain "hot" in name or description field and have status = 1 field and belong to category with id = 3 with paginate

实体

实体查询函数列表

将查询范围限定为仅包括特定类型的产品。

public function scopeOfType($query, $type)

按类型获取产品集合。

public static function getByType($type = 'products')

按类型和分页获取产品。

public static function getByTypeWithPagination($type = 'products', $per_page = 15)

按类型和ID获取产品。

public static function findByType($id, $type = 'products')

获取产品元数据。

public function getMetaField($key)

将查询范围限定为仅包括热门产品。

public function scopeIsHot($query)

将查询范围限定为仅包括有库存的产品。

public function scopeInStock($query)

将查询范围限定为仅包括已发布的产品。

public function scopeIsPublished($query)

将查询范围限定为按订单列排序产品。

public function scopeSortByOrder($query, $order = 'desc')

将查询范围限定为按发布日期列排序产品。

public function scopeSortByPublishedDate($query, $order = 'desc')

将查询范围限定为按销量排序产品。

public function scopeSortBySoldQuanlity($query, $order = 'desc')

将查询范围限定为搜索给定关键字的产品。此函数还可以与分类或标签一起使用。

public function scopeOfSearching($query, $search, $with_category = false, $with_tag = false)

将查询范围限定为包括相关产品。此函数还可以与分类或标签一起使用。

public function scopeOfRelatingTo($query, $product, $with_category = false, $with_tag = false)

使用实体查询函数

使用特性。

namespace App\Model;

use VCComponent\Laravel\Product\Traits\ProductQueryTrait;

class Product 
{
    use ProductQueryTrait;
    \\
}

扩展 VCComponent\Laravel\Product\Entities\Product 实体。

namespace App\Model;

use VCComponent\Laravel\Product\Entities\Product as BaseProduct;

class Product extends BaseProduct
{
    \\
}

实体查询函数示例

$product = Product::isPublished()->inStock()->with('categories')->sortBySoldQuanlity()->first();
$products = Product::ofType('product')->isPublished()->inStock()->ofRelatingTo($product, true)->get();

视图

您的 ProductListController 控制器类必须扩展 VCComponent\Laravel\Product\Http\Controllers\Web\ProductListController as BaseProductListController 并实现 VCComponent\Laravel\Product\Contracts\ViewProductListControllerInterface;

class ProductListController extends BaseProductListController implements ViewProductListControllerInterface
{
}

您的 ProductDetailController 控制器类必须扩展 VCComponent\Laravel\Product\Http\Controllers\Web\ProductDetailController as BaseProductDetailController 并实现 VCComponent\Laravel\Product\Contracts\ViewProductDetailControllerInterface;

class ProductDetailController extends BaseProductDetailController implements ViewProductDetailControllerInterface
{
}

如果您想更改默认视图,您必须将视图添加到 Product 控制器类中。

protected function view()
{
    return 'view-custom';
}

路由

API端点应具有以下格式

动词 URI
GET /api/{namespace}/admin/products
GET /api/{namespace}/admin/products/{id}
POST /api/{namespace}/admin/products
PUT /api/{namespace}/admin/products/{id}
DELETE /api/{namespace}/admin/products/{id}
PUT /api/{namespace}/admin/products/status/bulk
PUT /api/{namespace}/admin/products/status/{id}
---- ----
GET /api/{namespace}/
GET /api/{namespace}/{id}
POST /api/{namespace}/
PUT /api/{namespace}/{id}
DELETE /api/{namespace}/{id}
PUT /api/{namespace}/status/bulk
PUT /api/{namespace}/status/{id}