webpress / post-manager
This package's canonical repository appears to be gone and the package has been frozen as a result.
Installs: 2 582
Dependents: 3
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 8
Open Issues: 0
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
- Post Manager Package for Laravel
Post management package for managing post in laravel framework
Installation
Composer
To include the package in your project, Please run following command.
composer require vicoders/postmanager
Service Provider
In your config/app.php
add the following Service Providers to the end of the providers
array:
'providers' => [ ... VCComponent\Laravel\Post\Providers\PostComponentProvider::class, VCComponent\Laravel\Post\Providers\PostComponentRouteProvider::class, ],
Config and Migration
Run the following commands to publish configuration and migration files.
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"
Create tables:
php artisan migrate
Environment
In .env
file, we need some configuration.
API_PREFIX=api
API_VERSION=v1
API_NAME="Your API Name"
API_DEBUG=false
Configuration
URL namespace
To avoid duplication with your application's api endpoints, the package has a default namespace for its routes which is post-management
. For example:
{{url}}/api/post-management/admin/posts
You can modify the package url namespace to whatever you want by modifying the POST_COMPONENT_NAMESPACE
variable in .env
file.
POST_COMPONENT_NAMESPACE="your-namespace"
Model and Transformer
You can use your own model and transformer class by modifying the configuration file config\post.php
'models' => [ 'post' => App\Entities\Post::class, ], 'transformers' => [ 'post' => App\Transformers\PostTransformer::class, ],
Your Post
model class must implements VCComponent\Laravel\Post\Contracts\PostSchema
and 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', ]; }
Auth middleware
Configure auth middleware in configuration file config\post.php
'auth_middleware' => [ 'admin' => [ 'middleware' => 'jwt.auth', 'except' => ['index'], ], 'frontend' => [ 'middleware' => 'jwt.auth', 'except' => ['index'], ], ],
Query functions provide
Repository
List of query functions
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")
We also provide functions that return list paginated posts. In order to use those functions, just add 'paginated' to the functions return the list of posts you want to use.
For example: getListHotPosts
will be getListPaginatedPosts
.
Use
At controller use PostRepository
and add function __construct
use VCComponent\Laravel\Post\Repositories\PostRepository;
public function __construct(PostRepository $postRepo) { $this->postRepo = $postRepo; }
For example
$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
Entity
List of query functions-Entity
Scope a query to only include posts of a given type.
public function scopeOfType($query, $type)
Get post collection by type.
public static function getByType($type = 'posts')
Get post by type with pagination.
public static function getByTypeWithPagination($type = 'posts', $per_page = 15)
Get post by type and id.
public static function findByType($id, $type = 'posts')
Get post meta data.
public function getMetaField($key)
Scope a query to only include hot posts.
public function scopeIsHot($query)
Scope a query to only include publisded posts.
public function scopeIsPublished($query)
Scope a query to sort posts by order column.
public function scopeSortByOrder($query, $order = 'desc')
Scope a query to sort posts by published_date column.
public function scopeSortByPublishedDate($query, $order = 'desc')
Scope a query to search posts of given key word. This function is also able to scope with categories, or tags.
public function scopeOfSearching($query, $search, $with_category = false, $with_tag = false)
Scope a query to include related posts. This function is also able to scope with categories, or tags.
public function scopeOfRelatingTo($query, $post, $with_category = false, $with_tag = false)
Use-Entity
Use Trait.
namespace App\Model; use VCComponent\Laravel\Post\Traits\PostQueryTrait; class Post { use PostQueryTrait; \\ }
Extend VCComponent\Laravel\Post\Entities\Post
Entity.
namespace App\Model; use VCComponent\Laravel\Post\Entities\Post as BasePost; class Post extends BasePost { \\ }
For example-Entity
Post::ofType('posts')->IsPublised()->isHost()->search('Hello world', false, true)->sortByPublishedDate('desc')->paginate(12);
Table of contents
The package provides a function for posts
entity to get table of contents by posts content.
List of TOC functions
$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
Using of TOC
At Post
entity extend BasePost
use VCComponent\Laravel\Post\Entities\Post as BasePost; // [...] class Post extends BasePost { // [...] }
Or import TableOfContentsTrait and use it in Post
entity
use VCComponent\Laravel\Post\Traits\TableOfContentsTrait; class Post { use TableOfContentsTrait; }
The package also provide a styled view blade that can be included in post-detail page
@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.
Remember publish the PostComponentProvider
, and import scss file in the main css to use the default style css.
php artisan vendor:publish --provider="VCComponent\Laravel\Post\Providers\PostComponentProvider"
@import "./table_of_contents/table_of_contents.scss";
Example of 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
Post-type
By default, the package provide posts
post-type. If you want to define additional post-type
, feel free to add the post-type
name to postTypes()
method in your Post
model class.
public function postTypes() { return [ 'about', ]; }
If your post-type
has additional fields, just add the schema
of your post-type
to the Post
model class.
public function aboutSchema() { return [ 'information' => [ 'type' => 'text', 'rule' => ['nullable'], ], 'contact' => [ 'type' => 'text', 'rule' => ['required'], ], ]; }
By default, the package will show you a default view. If you want to change the view post-type
name to postTypes()
, just add the view
of your post-type
to the Post
controller class.
public function viewAbout() { return 'pages.about-view'; }
Routes
The api endpoint should have these format:
Verb | 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} |