webpress / category-manager
Requires
- cviebrock/eloquent-sluggable: 7.0.1
- doctrine/dbal: v2.12.1
- webpress/core: ^3.1
Requires (Dev)
- orchestra/testbench: 5.0.0
- dev-master
- 3.1.87
- 3.1.86
- 3.1.85
- 3.1.83
- 3.1.82
- 3.1.81
- 3.1.80
- 3.1.79
- 3.1.78
- 3.1.77
- 3.1.76
- 3.1.74
- 3.1.73
- 3.1.72
- 3.1.71
- 3.1.70
- 3.1.69
- 3.1.68
- 3.1.67
- 3.1.66
- 3.1.65
- 3.1.64
- 3.1.63
- 3.1.61
- 3.1.60
- 3.1.59
- 3.1.58
- 3.1.57
- 3.1.56
- 3.1.54
- 3.1.53
- 3.1.52
- 3.1.51
- 3.1.50
- 3.1.49
- 3.1.48
- 3.1.47
- 3.1.46
- 3.1.45
- 3.1.44
- 3.1.43
- 3.1.42
- 3.1.41
- 3.1.40
- 3.1.39
- 3.1.38
- 3.1.37
- 3.1.36
- 3.1.35
- 3.1.34
- 3.1.33
- 3.1.32
- 3.1.31
- 3.1.30
- 3.1.29
- 3.1.28
- 3.1.27
- 3.1.26
- 3.1.25
- 3.1.24
- 3.1.23
- 3.1.22
- 3.1.20
- 3.1.19
- 3.1.18
- 3.1.17
- 3.1.15
- 3.1.14
- 3.1.13
- 3.1.12
- 3.1.11
- 3.1.10
- 3.1.9
- 3.1.8
- 3.1.7
- 3.1.6
- 3.1.5
- 3.1.3
- 3.1.2
- 3.1.1
- 3.1.0
- 3.0.34
- 3.0.33
- 3.0.32
- 3.0.31
- 3.0.30
- 3.0.29
- 3.0.28
- 3.0.27
- 3.0.26
- 3.0.25
- 3.0.24
- 3.0.23
- 3.0.22
- 3.0.21
- 3.0.20
- 3.0.19
- 3.0.18
- 3.0.17
- 3.0.16
- 3.0.15
- 3.0.14
- 3.0.13
- 3.0.12
- 3.0.11
- 3.0.10
- 3.0.9
- 3.0.8
- 3.0.6
- 3.0.5
- 3.0.3
- 3.0.2
- 3.0.1
- 3.0.0
- 2.1.1
- 2.1.0
- 2.0.0
- 1.5.0
- 1.4.0
- 1.3.0
- 1.2.1
- 0.19.0
- 0.18.0
- 0.17.0
- 0.16.0
- 0.1.0
- 0.0.9
- 0.0.8
- 0.0.7
- 0.0.6
- 0.0.5
- 0.0.4
- 0.0.3
- 0.0.2
- 0.0.1
- dev-dev/v9.0
- dev-develop
- dev-dev_haivd
This package is auto-updated.
Last update: 2022-06-17 10:31:34 UTC
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\CategorySchema
和 VCComponent\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 { }
如果您想更改默认的 CategoryList
、CategoryDetail
视图,您必须将视图添加到 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} |