Moin Uddin Chowdhury
Sep 7 · 9 min read
<?php namespace App\Blog\AbstractModel;use App\Blog\Contracts\IConsume;abstract class AbstractConsumeModel implements IConsume {    /**
* Show records
*
* @return json object
*/
public function show(){}
/**
* Create record
*
* @param array $data
* @return void
*/
public function create(array $data){}
/**
* Update record
*
* @param [type] $id
* @return void
*/
public function update($id){}
/**
* Delete record
*
* @param [type] $id
* @return void
*/
public function delete($id){}
}
<?phpnamespace App\grubapi\Contracts;Interface IConsumer {     /**
* Show records
*
* @return json object
*/
public function show();
/**
* Create record
*
* @param array $data
* @return void
*/
public function create(array $data);
/**
* Update record
*
* @param [type] $id
* @return void
*/
public function update($id);
/**
* Delete records
*
* @param [type] $id
* @return void
*/
public function delete($id);
}
<?php 
namespace App\Blog\Render;
use App\Blog\AbstractModel\AbstractConsumeModel;
class Render {

protected $consume;
/**
* Pass AbstractComsumeModel instance
*
* @param AbstractComsumeModel $parameter
*/
public function __construct(AbstractConsumeModel $consume) {
$this->consume = $consume;
}

/**
* Access methods and properties of the parameters
*
* @return AbstractParameterModel
*/
public function renderResource():AbstractConsumeModel {
return $this->consume;
}
}
<?php
return [
‘blog’ => [
‘post’ => \App\blog\Consumes\Posts::class,
// other's may come depends on your needs
]
];
<?phpnamespace App\Blog\Consumes;
use App\Blog\AbstractModel\AbstractConsumeModel;
use App\Blog\Models\Post;
class Posts extends AbstractConsumeModel
{
/**
* Show records
*
* @return json object
*/
public function show()
{
$posts = Post::all();
return response()->json($posts, 200);
}
/**
* Create record
*
* @param array $data
* @return void
*/
public function create(array $data){}
/**
* Update record
*
* @param [type] $id
* @return void
*/
public function update($id){}
/**
* Delete record
*
* @param [type] $id
* @return void
*/
public function delete($id){}

}
<?phpnamespace App\Blog\Models;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
//
}
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('title');
$table->text('body');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
}
<?php
/** @var \Illuminate\Database\Eloquent\Factory $factory */
use App\Blog\Models\Post;
use Faker\Generator as Faker;
$factory->define(Post::class, function (Faker $faker) {
return [
'title' => $faker->sentence($nbWords = 6),
'body' => $faker->paragraph($nbSentences = 3)
];
});
<?phpuse Illuminate\Database\Seeder;use App\Blog\Models\Post;class PostsTableSeeder extends Seeder{    /**    * Run the database seeds.    *    * @return void    */    public function run()    {      factory(Post::class, 50)->create();    }}
<?phpuse Illuminate\Database\Seeder;class DatabaseSeeder extends Seeder
{
/** * Seed the application's database. * * @return void */ public function run() { $this->call(PostsTableSeeder::class); }}
<?php
namespace App\Blog;
use Illuminate\Support\ServiceProvider;
use App\Blog\Render\Render;
class BlogServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
$this->publishConfig();
//$this->publishView(); // if you want to use your views from your module
$this->loadMigration();
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->bind('resources', function($app, $consume) {
return new Render(new $consume['resource']);
});

}
/**
* Publish package config file to framework config folder
*/
protected function publishConfig() {

$this->publishes([
__DIR__.'/config/blog.php' => config_path('blog.php')
]);
$this->mergeConfigFrom(__DIR__.'/config/blog.php', 'blog');

}
/**
* Load application view

public function publishView() {
$this->loadViewsFrom(__DIR__.'/views', 'view');
}
*/
/**
* Load Application migration to create database
*/
public function loadMigration() {
$this->publishes([
__DIR__.'/migrations/' => database_path('migrations')
]);
$this->loadMigrationsFrom(__DIR__.'/migrations');
}
}
"autoload": {    "psr-4": {        "App\\": "app/"    },
}
'providers' => [    .....    App\Blog\BlogServiceProvider::class,],
Which provider or tag's files would you like to publish?:[0 ] Publish files from all providers and tags listed below[1 ] Provider: App\Blog\BlogServiceProvider[2 ] Provider: Fideloper\Proxy\TrustedProxyServiceProvider[3 ] Provider: Illuminate\Foundation\Providers\FoundationServiceProvider[4 ] Provider: Illuminate\Mail\MailServiceProvider[5 ] Provider: Illuminate\Notifications\NotificationServiceProvider[6 ] Provider: Illuminate\Pagination\PaginationServiceProvider[7 ] Provider: Laravel\Tinker\TinkerServiceProvider[8 ] Tag: laravel-errors[9 ] Tag: laravel-mail[10] Tag: laravel-notifications[11] Tag: laravel-pagination
<?phpnamespace App\Blog\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Requests;
use App;
class PostController extends Controller
{
protected $rescource;
//protected $category; //If we have category
/**
* Get parameters instance from IOC container
*/
public function __construct()
{
$this->rescource = App::make('resources', [
'resource' => config('blog.blog.post')
]);
/** If you have a category class than /*$this->category = App::make('resources', [
'resource' => config('blog.blog.category')
]);
*/
}
/**
*
* @param Request request
*
* @return json object
*/
public function showPosts(Request $request) {
return $this->rescource
->renderResource()
->show();
}
/**
* If we want to show category using IOC container
* @param Request request
*
* @return json object

public function showCategory(Request $request) {
return $this->category
->renderResource()
->show();
}
*/}

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade