webpress / post-manager
Requires
- caseyamcl/toc: 2.3.1
- cviebrock/eloquent-sluggable: 7.0.1
- webpress/category-manager: ^3.1
- webpress/core: ^3.1
- webpress/tag-manager: ^3.1
- webpress/user-manager: ^3.1
- webpress/view-model: ^3.1
Requires (Dev)
- orchestra/testbench: 5.0.0
- phpunit/phpunit: 8.5.13
- 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.3.0
- 0.2.1
- 0.2.0
- 0.1.6
- 0.1.5
- 0.1.4
- 0.1.3
- 0.1.2
- 0.1.1
- 0.1.0
- 0.0.11
- 0.0.10
- 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-feat/optimize-post-list-api
- dev-feat/post-meta-api
- dev-revert-82-revert-81-develop
- dev-revert-81-develop
- dev-develop
- dev-dev/v9.0
- dev-feature/dynamic-post-schema
- dev-feature/integration-test-api
- dev-feature/post-type-meta
This package is auto-updated.
Last update: 2022-06-17 10:31:33 UTC
README
Laravel 框架中的帖子管理包
安装
Composer
要将包包含到您的项目中,请运行以下命令。
composer require vicoders/postmanager
服务提供者
在您的 config/app.php
中,将以下服务提供者添加到 providers
数组的末尾
'providers' => [ ... VCComponent\Laravel\Post\Providers\PostComponentProvider::class, VCComponent\Laravel\Post\Providers\PostComponentRouteProvider::class, ],
配置和迁移
运行以下命令以发布配置和迁移文件。
php artisan vendor:publish --provider="VCComponent\Laravel\Post\Providers\PostComponentProvider"
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 端点重复,该包具有默认的命名空间 post-management
。例如
{{url}}/api/post-management/admin/posts
您可以通过修改 .env
文件中的 POST_COMPONENT_NAMESPACE
变量来将包的 URL 命名空间更改为您想要的任何内容。
POST_COMPONENT_NAMESPACE="your-namespace"
模型和转换器
您可以通过修改配置文件 config\post.php
来使用自己的模型和转换器类
'models' => [ 'post' => App\Entities\Post::class, ], 'transformers' => [ 'post' => App\Transformers\PostTransformer::class, ],
您的 Post
模型类必须实现 VCComponent\Laravel\Post\Contracts\PostSchema
和 VCComponent\Laravel\Post\Contracts\PostManagement
<?php namespace App\Entities; use Illuminate\Database\Eloquent\Model; use Prettus\Repository\Contracts\Transformable; use Prettus\Repository\Traits\TransformableTrait; use VCComponent\Laravel\Post\Contracts\PostManagement; use VCComponent\Laravel\Post\Contracts\PostSchema; use VCComponent\Laravel\Post\Traits\PostManagementTrait; use VCComponent\Laravel\Post\Traits\PostSchemaTrait; class Post extends Model implements Transformable, PostSchema, PostManagement { use TransformableTrait, PostSchemaTrait, PostManagementTrait; const STATUS_PENDING = 1; const STATUS_ACTIVE = 2; protected $fillable = [ 'title', 'description', 'content', ]; }
认证中间件
在配置文件 config\post.php
中配置认证中间件
'auth_middleware' => [ 'admin' => [ 'middleware' => 'jwt.auth', 'except' => ['index'], ], 'frontend' => [ 'middleware' => 'jwt.auth', 'except' => ['index'], ], ],
查询函数提供
仓库
查询函数列表
public function getPostWithMetas($id) public function getPostWithCategories($id) public function getPostWithCommnets($id) public function getPostWithTags($id) public function getPostWithMetasCategories($id) public function getPostWithMetasComments($id) public function getPostWithMetasTags($id) public function getPostWithCategoriesComments($id) public function getPostWithCategoriesTags($id) public function getPostWithCommentsTags($id) public function getPostWithAll($id) public function getListPostsHasCategories($number_of_posts = null, $type = "posts") public function getListPostsHasTags($number_of_posts = null, $type = "posts") public function getListPostsWithMetas($number_of_posts = null, $type = "posts") public function getListPostsWithCategories($number_of_posts = null, $type = "posts") public function getListPostsWithMetasCategories($number_of_posts = null, $type = "posts") public function getListHotPosts($number_of_posts = null, $type = "posts") public function getListHotPostsHasCategories($number_of_posts = null, $type = "posts") public function getListHotPostsHasTags($number_of_posts = null, $type = "posts") public function getListHotPostsWithMetas($number_of_posts = null, $type = "posts") public function getListHotPostsWithCategories($number_of_posts = null, $type = "posts") public function getListHotPostsWithMetasCategories($number_of_posts = null, $type = "posts") public function getListRelatedPosts($post, $number_of_posts = null, $type = "posts") public function getListRelatedPostsWithMetas($post, $number_of_posts = null, $type = "posts") public function getListRelatedPostsWithCategories($post, $number_of_posts = null, $type = "posts") public function getListRelatedPostsWithMetasCategories($post, $number_of_posts = null, $type = "posts") public function getListOfSearchingPosts($search, $number_of_posts = null, $type = "posts") public function getListOfSearchingPostsHasCategoriesTags($search, $number_of_posts = null, $type = "posts") public function getListOfSearchingPostsWithMetas($search, $number_of_posts = null, $type = "posts") public function getListOfSearchingPostsWithCategories($search, $number_of_posts = null, $type = "posts") public function getListOfSearchingPostsHasCategoriesTagsWithMetas($search, $number_of_posts = null, $type = "posts") public function getListOfSearchingPostsHasCategoriesTagsWithMetasCategories($search, $number_of_posts = null, $type = "posts") public function getListTranslatablePosts($number_of_posts = null, $type = "posts")
我们还提供了返回分页帖子列表的函数。为了使用这些函数,只需将 '分页' 添加到返回您想要使用的帖子列表的函数中。例如:getListHotPosts
将变为 getListPaginatedPosts
。
使用
在控制器中使用 PostRepository
并添加函数 __construct
use VCComponent\Laravel\Post\Repositories\PostRepository;
public function __construct(PostRepository $postRepo) { $this->postRepo = $postRepo; }
例如
$postField = $this->postRepo->findByField('title', 'about'); // get the post of the post type posts with the title about $postWhere = $this->postRepo->findByWhere(['status' => 1, 'is_hot' => 1]); // get posts belonging to post type posts with field is_hot = 1 and status = 1 $postWhere = $this->postRepo->findByWhere(['status' => 1, 'is_hot' => 1]); // get posts belonging to post type posts with field is_hot = 1 and status = 1 with paginate $postsType = $this->postRepo->getPostsAll('about'); // get articles belonging to post type about $postById = $this->postRepo->getPostByID(1); // get posts with id = 1 $postById = $this->postRepo->getPostMedias(2) // get a list of images of posts with id = 2 $postUrl = $this->postRepo->getPostUrl(1); // get the post link with id = 1 $postRelated = $this->postRepo->getRelatedPosts(2, ['type' => 'about'], 0); // get all posts related to post with id = 2 and belong to post type about $postRelatedPaginate = $this->postRepo->getRelatedPosts(2, ['type' => 'about']); // get all posts that are related to post with id = 2 and belong to post type about with pagination $postWithCategory = $this->postRepo->getPostsWithCategory(2, ['status' => 1]); // get all posts in category id = 2 and posts with field status = 1 $postWithCategoryPaginate = $this->postRepo->getPostsWithCategoryPaginate(2, ['status' => 1]); // get all posts of category id = 2 and posts with field status = 1 with pagination $postResult = $this->postRepo->getSearchResult('hi', ['title','content'],['type' => 'posts']); // get all posts that contain "hi" in title or content field and belong to post type posts $postResult = $this->postRepo->getSearchResult('hi', ['title','content'],['status' => 1],3); // get all posts that contain "hi" in title or content field and have status = 1 field and belong to category with id = 3 $postResult = $this->postRepo->getSearchResultPaginate('hi', ['title','content'],['status' => 1],3); // get all posts that contain "hi" in title or content field and have status = 1 field and belong to category with id = 3 with pagination
实体
查询函数列表-实体
将查询范围仅限于给定类型的帖子。
public function scopeOfType($query, $type)
通过类型获取帖子集合。
public static function getByType($type = 'posts')
通过类型和分页获取帖子。
public static function getByTypeWithPagination($type = 'posts', $per_page = 15)
通过类型和ID获取帖子。
public static function findByType($id, $type = 'posts')
获取帖子元数据。
public function getMetaField($key)
将查询范围仅限于热门帖子。
public function scopeIsHot($query)
将查询范围仅限于已发布的帖子。
public function scopeIsPublished($query)
将查询范围仅限于按排序列排序的帖子。
public function scopeSortByOrder($query, $order = 'desc')
将查询范围仅限于按发布日期列排序的帖子。
public function scopeSortByPublishedDate($query, $order = 'desc')
将查询范围仅限于给定关键词的帖子。此功能还可以与类别或标签一起使用。
public function scopeOfSearching($query, $search, $with_category = false, $with_tag = false)
将查询范围仅限于相关帖子。此功能还可以与类别或标签一起使用。
public function scopeOfRelatingTo($query, $post, $with_category = false, $with_tag = false)
使用-实体
使用特性。
namespace App\Model; use VCComponent\Laravel\Post\Traits\PostQueryTrait; class Post { use PostQueryTrait; \\ }
扩展 VCComponent\Laravel\Post\Entities\Post
实体。
namespace App\Model; use VCComponent\Laravel\Post\Entities\Post as BasePost; class Post extends BasePost { \\ }
例如-实体
Post::ofType('posts')->IsPublised()->isHost()->search('Hello world', false, true)->sortByPublishedDate('desc')->paginate(12);
目录
该包为 posts
实体提供了一个通过帖子内容获取目录的功能。
TOC函数列表
$toc = $this->getTableOfContents(2, 4); // get a html source code of the table of contents which has top level header equals to <h2> and has 4 levels depth
TOC的使用
在 Post
实体中扩展 BasePost
use VCComponent\Laravel\Post\Entities\Post as BasePost; // [...] class Post extends BasePost { // [...] }
或导入 TableOfContentsTrait 并在 Post
实体中使用它
use VCComponent\Laravel\Post\Traits\TableOfContentsTrait; class Post { use TableOfContentsTrait; }
该包还提供了一个可以包含在帖子详情页面中的样式化视图 blade。
@include('post-manager::table-of-contents', ['custom_class' => 'default_table_of_contents','top_level' => 1, 'depth' => 6]) //Variables are not necessary to be provided, all variables has their default value.
请记住发布 PostComponentProvider
,并在主 css 中导入 scss 文件以使用默认样式 css。
php artisan vendor:publish --provider="VCComponent\Laravel\Post\Providers\PostComponentProvider"
@import "./table_of_contents/table_of_contents.scss";
TOC示例
@include('post-manager::table-of-contents', ['custom_class' => 'default_table_of_contents','top_level' => 1, 'depth' => 6]) //get a table of contents with header is h1 and 6 level depth
帖子类型
默认情况下,该包提供 posts
帖子类型。如果您想定义其他 post-type
,请随意将 post-type
名称添加到您的 Post
模型类中的 postTypes
方法。
public function postTypes() { return [ 'about', ]; }
如果您的 post-type
有其他字段,只需将您的 post-type
的 schema
添加到 Post
模型类中。
public function aboutSchema() { return [ 'information' => [ 'type' => 'text', 'rule' => ['nullable'], ], 'contact' => [ 'type' => 'text', 'rule' => ['required'], ], ]; }
默认情况下,该包将显示默认视图。如果您想更改视图并将 post-type
名称更改为 postTypes
,只需将您的 post-type
视图添加到 Post
控制器类中。
public function viewAbout() { return 'pages.about-view'; }
路由
API端点应具有以下格式
动词 | URI |
---|---|
GET | /api/{namespace}/admin/{post-type} |
GET | /api/{namespace}/admin/{post-type}/{id} |
POST | /api/{namespace}/admin/{post-type} |
PUT | /api/{namespace}/admin/{post-type}/{id} |
DELETE | /api/{namespace}/admin/{post-type}/{id} |
PUT | /api/{namespace}/admin/{post-type}/status/bulk |
PUT | /api/{namespace}/admin/{post-type}/status/{id} |
GET | /api/{namespace}/admin/{post-type}/{id}/blocks |
---- | ---- |
GET | /api/{namespace}/{post-type} |
GET | /api/{namespace}/{post-type}/{id} |
POST | /api/{namespace}/{post-type} |
PUT | /api/{namespace}/{post-type}/{id} |
DELETE | /api/{namespace}/{post-type}/{id} |
PUT | /api/{namespace}/{post-type}/status/bulk |
PUT | /api/{namespace}/{post-type}/status/{id} |