webpress / post-manager

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

3.1.87 2022-06-15 03:30 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\PostSchemaVCComponent\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-typeschema 添加到 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}