webpress/category-manager

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


README

Laravel 框架中管理分类的分类管理包

安装

Composer

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

composer require vicoders/categorymanager

服务提供者

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

'providers' => [
        ...
    VCComponent\Laravel\Category\Providers\CategoryServiceProvider::class,
    VCComponent\Laravel\Category\Providers\CategoryRouteProvider::class,
],

配置和迁移

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

php artisan vendor:publish --provider="VCComponent\Laravel\Category\Providers\CategoryServiceProvider"
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 端点重复,此包为其路由具有默认命名空间 category-management。例如

{{url}}/api/category-management/admin/categories

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

CATEGORY_COMPONENT_NAMESPACE="your-namespace"

模型和转换器

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

'models'          => [
    'category' => App\Entities\Category::class,
],

'transformers'    => [
    'category' => App\Transformers\CategoryTransformer::class,
],

您的 Category 模型类必须实现 VCComponent\Laravel\Category\Contracts\CategorySchemaVCComponent\Laravel\Category\Contracts\CategoryManagement

<?php

namespace App\Entities;

use Illuminate\Database\Eloquent\Model;
use Prettus\Repository\Contracts\Transformable;
use Prettus\Repository\Traits\TransformableTrait;
use VCComponent\Laravel\Category\Contracts\CategoryManagement;
use VCComponent\Laravel\Category\Contracts\CategorySchema;
use VCComponent\Laravel\Category\Traits\CategoryManagementTrait;
use VCComponent\Laravel\Category\Traits\CategorySchemaTrait;

class Category extends Model implements Transformable, CategorySchema, CategoryManagement
{
    use TransformableTrait, CategorySchemaTrait, CategoryManagementTrait;

    const STATUS_PENDING = 1;
    const STATUS_ACTIVE  = 2;

    protected $fillable = [
        'name',
        'slug',
        'parent_id',
        'type',
        'type_post'
    ];
}

认证中间件

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

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

提供查询函数

仓库

查询函数列表

获取文章类型的分类列表

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

public function getCategoriesQueryPaginate(array $where, $number = 10, $order_by ='order', $order = 'asc', $columns = ['*']);
// Get a list of categories of a paginated post type

获取文章的分类列表

public function getPostCategoriesQuery($post_id, array $where, $post_type = 'posts', $number = 10, $order_by = 'order', $order = 'asc');

public function getPostCategoriesQueryPaginate($post_id, array $where, $post_type = 'posts', $number = 10, $order_by = 'order', $order = 'asc');
// get the category list of a paginated article

使用

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

use VCComponent\Laravel\Category\Repositories\CategoryRepository;
public function __construct(CategoryRepository $categoryRepo)
{
    $this->categoryRepo = $categoryRepo;
}

例如

public function index() {
    $categories = $this->categoryRepo->getCategoriesQuery(['type'=>'knowledge'],0);
    // get all categories of post type knowledge (with $number = 0 get all records)
    $categoriesPaginate = $this->categoryRepo
    ->getCategoriesQueryPaginate(['type'=>'knowledge']);
    // get categories of paginated  knowledge post type
    $postCategories = $this->categoryRepo->getPostCategoriesQuery(45,['status'=>1]);
    // retrieve the categories of posts id = 45
    $postCategoriesPaginate = $this->categoryRepo
    ->getPostCategoriesQueryPaginate(45,['status'=>1]);
    // get the categories of posts id = 45 with pagination
}

实体

实体查询函数列表

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

public function scopeOfType($query)

将查询范围限定为仅包括热门类别。

public function scopeIsHot($query)

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

public function scopeIsPublished($query)

将查询范围限定为按顺序列对类别进行排序。

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

将查询范围限定为按名称列对类别进行排序。

public function scopeSortByName($query, $order = 'asc')

将查询范围限定为按使用时间对类别进行排序。从高到低。

public function scopeMostUsed($query, $categoryable_type = null)

将查询范围限定为按使用时间对类别进行排序。从低到高。

public function scopeLeastUsed($query, $categoryable_type = null)

使用实体查询函数

使用特质。

namespace App\Model;

use VCComponent\Laravel\Category\Traits\CategoryQueryTrait;

class Category 
{
    use CategoryQueryTrait;
    \\
}

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

namespace App\Model;

use VCComponent\Laravel\Category\Entities\Category as BaseCategory;

class Category extends BaseCategory
{
    \\
}

实体查询函数示例

$category = Category::ofType('posts')->isHot()->isPublished()->mostUsed()->pageinate(15);

视图

您的 CategoryListController 控制器类必须继承 VCComponent\Laravel\Category\Http\Controllers\Web\CategoryListController as BaseCategoryListController 并实现 VCComponent\Laravel\Category\Contracts\ViewCategoryListControllerInterface;

class CategoryListController extends BaseCategoryListController implements ViewCategoryListControllerInterface
{
}

您的 CategoryDetailController 控制器类必须继承 VCComponent\Laravel\Category\Http\Controllers\Web\CategoryDetailController as BaseCategoryDetailController 并实现 VCComponent\Laravel\Category\Contracts\ViewCategoryDetailControllerInterface;

class CategoryDetailController extends BaseCategoryDetailController implements ViewCategoryDetailControllerInterface
{
}

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

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

路由

API端点应该具有以下格式

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