Skip to content

Instantly share code, notes, and snippets.

@juyal-ahmed
Last active May 8, 2019 13:51
Show Gist options
  • Save juyal-ahmed/126a03c19f7bd048cab4ddbe6f308178 to your computer and use it in GitHub Desktop.
Save juyal-ahmed/126a03c19f7bd048cab4ddbe6f308178 to your computer and use it in GitHub Desktop.
Creating a package with Laravel 5.6 for a printing company ex: Alpha and the package name ex: Library.
{
"autoload": {
"classmap": [
"database/seeds",
"database/factories"
],
"psr-4": {
"App\\": "app/",
"Alpha\\Library\\": "packages/Library/src/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/",
"Alpha\\Tests\\": "packages/Library/tests/"
}
}
}
<?php
return [
/*
|--------------------------------------------------------------------------
| Autoloaded Service Providers
|--------------------------------------------------------------------------
|
| The service providers listed here will be automatically loaded on the
| request to your application. Feel free to add your own services to
| this array to grant expanded functionality to your applications.
|
*/
'providers' => [
/*
* Laravel Framework Service Providers...
*/
/**
* Alpha Service Providers
*/
Alpha\Library\LibraryServiceProvider::class,
],
];
{
"name": "alpha/library",
"description": "Library package to manage all library resource related features.",
"authors": [
{
"name": "Juyal Ahmed",
"email": "[email protected]"
}
],
"autoload": {
"psr-4": {
"Alpha\\Library\\": "src/"
}
},
"minimum-stability": "dev",
"require": {
}
}
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Alpha\Library\Model\Author;
class CreateAuthorsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('authors', function (Blueprint $table) {
$table->increments('id');
$table->string('name', 120);
$table->text('description')->nullable();
$table->enum('status', Author::STATUSES)->nullable()->default(Author::STATUSES[0]);
$table->date('joined_date')->nullable();
$table->softDeletes();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('authors');
}
}
?>

Library Package Available API methods

Library package to manage all human resource related features for Alpha Prakashoni.

Author: Juyal Ahmed
Email: [email protected]
Last Modified: 08.May.2019

AuthorsController

Display a listing of the resource.
GET /api/library/authors
ResponseBody:

[
    {
        "id": 1,
        "name": "Juyal Ahmed",
        "description": "A programming or personal article author.",
        "status": "Active",
        "joined_date": "2020-06-10",
        "deleted_at": null,
        "created_at": "2019-05-06 16:56:51",
        "updated_at": "2019-05-06 16:57:20"
    }
]
<?php
namespace Alpha\Library\Http\Controllers\API;
use App\Http\Controllers\Controller;
use Alpha\Library\Http\Requests\Authors\AuthorRequest;
use Alpha\Library\Model\Author;
use Illuminate\Http\Response;
class AuthorsController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return response()->json(Author::all(), Response::HTTP_OK);
}
/**
* Store a newly created resource in storage.
*
* @param App\Http\Requests\Authors\AuthorRequest $request
* @return \Illuminate\Http\Response
*/
public function store(AuthorRequest $request)
{
$input = $request->only(['name', 'description', 'status', 'joined_date']);
$author = Author::create($input);
return response()->json(['success' => true, 'data' => $author], Response::HTTP_CREATED);
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
return response()->json(Author::findOrFail($id));
}
/**
* Update the specified resource in storage.
*
* @param App\Http\Requests\Authors\AuthorRequest $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(AuthorRequest $request, $id)
{
$input = $request->only(['name', 'description', 'status', 'joined_date']);
$author = Author::findOrFail($id);
$author->update($input);
return response()->json(['success' => true, 'data' => $author], Response::HTTP_OK);
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$author = Author::findOrFail($id);
$author->delete();
return response()->json(['success' => true, 'data' => null], Response::HTTP_NO_CONTENT);
}
/**
* Display a dropdown with id and name field of the resource.
*
* @return \Illuminate\Http\Response
*/
public function dropdown()
{
return response()->json(Author::where('status', Author::STATUS_ACTIVE)->get()->pluck(['id', 'name']), Response::HTTP_OK);
}
}
?>
<?php
namespace Alpha\Library\Http\Requests\Authors;
use Illuminate\Foundation\Http\FormRequest;
class AuthorRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required|max:120',
'status' => 'nullable|in:' . implode(',', Author::STATUSES),
'joined_date' => 'required|date_format:Y-m-d'
];
}
}
?>
<?php
namespace Alpha\Library;
use Illuminate\Routing\Router;
use Illuminate\Support\ServiceProvider;
class LibraryServiceProvider extends ServiceProvider
{
/**
*
* @param Router $router
* @return void
*/
public function boot(Router $router)
{
$this->loadRoutesFrom(__DIR__ . '/routes.php');
$this->loadMigrationsFrom(__DIR__ . '/../database/migrations');
}
/**
*
*
* @return void
*/
public function register()
{
}
}
<?php
namespace Alpha\Library\Model;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Author extends Model
{
use SoftDeletes;
/**
* Lists of available statuses where first index is the default by migration.
* Note: If you update this lists of Enum, please make sure you have created a migration for that accordingly.
*/
const STATUSES = ['Active', 'Inactive', 'Hold'];
const STATUS_ACTIVE = 'Active';
protected $table = 'authors';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
'description',
'status',
'joined_date'
];
}
<?php
use Illuminate\Support\Facades\Route;
use Alpha\Library\Http\Controllers\AuthorsController;
Route::prefix('api/library')
->middleware(['api', 'auth:api'])
->namespace(null)
->group(function () {
Route::get('/authors', AuthorsController::class . '@index');
Route::get('/authors/dropdown', AuthorsController::class . '@dropdown');
Route::get('/authors/{id}', AuthorsController::class . '@show');
Route::post('/authors', AuthorsController::class . '@store');
Route::delete('/authors/{id}', AuthorsController::class . '@destroy');
Route::put('/authors/{id}', AuthorsController::class . '@update');
});
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment