|
| 1 | +# Post Manager Package for Laravel |
| 2 | + |
| 3 | +- [Post Manager Package for Laravel](#post-manager-package-for-laravel) |
| 4 | + - [Installation](#installation) |
| 5 | + - [Composer](#composer) |
| 6 | + - [Service Provider](#service-provider) |
| 7 | + - [Config and Migration](#config-and-migration) |
| 8 | + - [Environment](#environment) |
| 9 | + - [Configuration](#configuration) |
| 10 | + - [URL namespace](#url-namespace) |
| 11 | + - [Model and Transformer](#model-and-transformer) |
| 12 | + - [Auth middleware](#auth-middleware) |
| 13 | + - [Post-type](#post-type) |
| 14 | + - [Routes](#routes) |
| 15 | + |
| 16 | + |
| 17 | +Post management package for managing post in laravel framework |
| 18 | + |
| 19 | +## Installation |
| 20 | + |
| 21 | +### Composer |
| 22 | + |
| 23 | +To include the package in your project, Please run following command. |
| 24 | + |
| 25 | +``` |
| 26 | +composer require vicoders/postmanager |
| 27 | +``` |
| 28 | + |
| 29 | +### Service Provider |
| 30 | + |
| 31 | +In your `config/app.php` add the following Service Providers to the end of the `providers` array: |
| 32 | + |
| 33 | +```php |
| 34 | +'providers' => [ |
| 35 | + ... |
| 36 | + VCComponent\Laravel\Post\Providers\PostComponentProvider::class, |
| 37 | + VCComponent\Laravel\Post\Providers\PostComponentRouteProvider::class, |
| 38 | +], |
| 39 | +``` |
| 40 | + |
| 41 | +### Config and Migration |
| 42 | + |
| 43 | +Run the following commands to publish configuration and migration files. |
| 44 | + |
| 45 | +``` |
| 46 | +php artisan vendor:publish --provider="VCComponent\Laravel\Post\Providers\PostComponentProvider" |
| 47 | +php artisan vendor:publish --provider="Dingo\Api\Provider\LaravelServiceProvider" |
| 48 | +php artisan vendor:publish --provider "Prettus\Repository\Providers\RepositoryServiceProvider" |
| 49 | +``` |
| 50 | +Create tables: |
| 51 | + |
| 52 | +``` |
| 53 | +php artisan migrate |
| 54 | +``` |
| 55 | + |
| 56 | +### Environment |
| 57 | + |
| 58 | +In `.env` file, we need some configuration. |
| 59 | + |
| 60 | +``` |
| 61 | +API_PREFIX=api |
| 62 | +API_VERSION=v1 |
| 63 | +API_NAME="Your API Name" |
| 64 | +API_DEBUG=false |
| 65 | +``` |
| 66 | + |
| 67 | +## Configuration |
| 68 | + |
| 69 | +### URL namespace |
| 70 | + |
| 71 | +To avoid duplication with your application's api endpoints, the package has a default namespace for its routes which is `post-management`. For example: |
| 72 | + |
| 73 | + {{url}}/api/post-management/admin/posts |
| 74 | +You can modify the package url namespace to whatever you want by modifying the `POST_COMPONENT_NAMESPACE` variable in `.env` file. |
| 75 | + |
| 76 | + POST_COMPONENT_NAMESPACE="your-namespace" |
| 77 | + |
| 78 | +### Model and Transformer |
| 79 | + |
| 80 | +You can use your own model and transformer class by modifying the configuration file `config\post.php` |
| 81 | + |
| 82 | +```php |
| 83 | +'models' => [ |
| 84 | + 'post' => App\Entities\Post::class, |
| 85 | +], |
| 86 | + |
| 87 | +'transformers' => [ |
| 88 | + 'post' => App\Transformers\PostTransformer::class, |
| 89 | +], |
| 90 | +``` |
| 91 | +Your `Post` model class must implements `VCComponent\Laravel\Post\Contracts\PostSchema` and `VCComponent\Laravel\Post\Contracts\PostManagement` |
| 92 | + |
| 93 | +```php |
| 94 | +<?php |
| 95 | + |
| 96 | +namespace App\Entities; |
| 97 | + |
| 98 | +use Illuminate\Database\Eloquent\Model; |
| 99 | +use Prettus\Repository\Contracts\Transformable; |
| 100 | +use Prettus\Repository\Traits\TransformableTrait; |
| 101 | +use VCComponent\Laravel\Post\Contracts\PostManagement; |
| 102 | +use VCComponent\Laravel\Post\Contracts\PostSchema; |
| 103 | +use VCComponent\Laravel\Post\Traits\PostManagementTrait; |
| 104 | +use VCComponent\Laravel\Post\Traits\PostSchemaTrait; |
| 105 | + |
| 106 | +class Post extends Model implements Transformable, PostSchema, PostManagement |
| 107 | +{ |
| 108 | + use TransformableTrait, PostSchemaTrait, PostManagementTrait; |
| 109 | + |
| 110 | + const STATUS_PENDING = 1; |
| 111 | + const STATUS_ACTIVE = 2; |
| 112 | + |
| 113 | + protected $fillable = [ |
| 114 | + 'title', |
| 115 | + 'description', |
| 116 | + 'content', |
| 117 | + ]; |
| 118 | +} |
| 119 | +``` |
| 120 | + |
| 121 | +### Auth middleware |
| 122 | + |
| 123 | +Configure auth middleware in configuration file `config\post.php` |
| 124 | + |
| 125 | +```php |
| 126 | +'auth_middleware' => [ |
| 127 | + 'admin' => [ |
| 128 | + 'middleware' => 'jwt.auth', |
| 129 | + 'except' => ['index'], |
| 130 | + ], |
| 131 | + 'frontend' => [ |
| 132 | + 'middleware' => 'jwt.auth', |
| 133 | + 'except' => ['index'], |
| 134 | + ], |
| 135 | +], |
| 136 | +``` |
| 137 | + |
| 138 | +## Post-type |
| 139 | + |
| 140 | +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. |
| 141 | +```php |
| 142 | +public function postTypes() |
| 143 | +{ |
| 144 | + return [ |
| 145 | + 'about', |
| 146 | + ]; |
| 147 | +} |
| 148 | +``` |
| 149 | +If your `post-type` has additional fields, just add the `schema` of your `post-type` to the `Post` model class. |
| 150 | + |
| 151 | +```php |
| 152 | +public function aboutSchema() |
| 153 | +{ |
| 154 | + return [ |
| 155 | + 'information' => [ |
| 156 | + 'type' => 'text', |
| 157 | + 'rule' => ['nullable'], |
| 158 | + ], |
| 159 | + 'contact' => [ |
| 160 | + 'type' => 'text', |
| 161 | + 'rule' => ['required'], |
| 162 | + ], |
| 163 | + ]; |
| 164 | +} |
| 165 | +``` |
| 166 | +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. |
| 167 | + |
| 168 | +```php |
| 169 | +public function viewAbout() |
| 170 | +{ |
| 171 | + return 'pages.about-view'; |
| 172 | +} |
| 173 | +``` |
| 174 | + |
| 175 | +## Routes |
| 176 | + |
| 177 | +The api endpoint should have these format: |
| 178 | +| Verb | URI | |
| 179 | +| ------ | ---------------------------------------------- | |
| 180 | +| GET | /api/{namespace}/admin/{post-type} | |
| 181 | +| GET | /api/{namespace}/admin/{post-type}/{id} | |
| 182 | +| POST | /api/{namespace}/admin/{post-type} | |
| 183 | +| PUT | /api/{namespace}/admin/{post-type}/{id} | |
| 184 | +| DELETE | /api/{namespace}/admin/{post-type}/{id} | |
| 185 | +| PUT | /api/{namespace}/admin/{post-type}/status/bulk | |
| 186 | +| PUT | /api/{namespace}/admin/{post-type}/status/{id} | |
| 187 | +| ---- | ---- | |
| 188 | +| GET | /api/{namespace}/{post-type} | |
| 189 | +| GET | /api/{namespace}/{post-type}/{id} | |
| 190 | +| POST | /api/{namespace}/{post-type} | |
| 191 | +| PUT | /api/{namespace}/{post-type}/{id} | |
| 192 | +| DELETE | /api/{namespace}/{post-type}/{id} | |
| 193 | +| PUT | /api/{namespace}/{post-type}/status/bulk | |
| 194 | +| PUT | /api/{namespace}/{post-type}/status/{id} | |
0 commit comments