1. Go to this page and download the library: Download deyan-ardi/laravel-layerize library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
deyan-ardi / laravel-layerize example snippets
composer
composer update deyan-ardi/laravel-layerize
php artisan layerize:dto User/StoreUserRequest
namespace App\Http\Requests\User;
use Illuminate\Foundation\Http\FormRequest;
class StoreUserRequest extends FormRequest
{
// Dto/validation with example implementation
public function authorize()
{
return true;
}
public function rules()
{
return [
// Validation rules
];
}
protected function prepareForValidation()
{
$this->merge([
// prepare for validation
]);
}
public function messages()
{
return [
// custom message of validation
];
}
public function attributes()
{
return [
// custom attributes of validation
];
}
}
php artisan layerize:controller UserController
namespace App\Http\Controllers;
use App\Helpers\Json;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Throwable;
class UserController extends Controller
{
// Controller code with example implementation
public function __construct(
// protected UserUseCase $userUseCase,
) {
}
public function index()
{
// Render from use case
// return $this->userUseCase->renderIndex();
}
public function datatable(Request $request)
{
try {
// Render from use case
// DB::beginTransaction();
// $datatable = $this->userUseCase->renderDatatable($request);
// DB::commit();
// return $datatable;
} catch (Throwable $th) {
// DB::rollBack();
// return Json::error($th->getMessage());
}
}
public function create()
{
// Render from use case
// return $this->userUseCase->renderCreate();
}
// StoreUserRequest can be generate using layerize:dto
public function store(StoreUserRequest $request)
{
try {
// Render from use case
// DB::beginTransaction();
// $this->userUseCase->execStore($request);
// DB::commit();
// return redirect()->back()->with('success', "Data Successfully Added");
} catch(Throwable $th) {
// DB::rollBack();
// return redirect()->back()->with('error', $th->getMessage());
}
}
public function edit(string $id)
{
// Render from use case
// return $this->userUseCase->renderEdit($id);
}
// UpdateUserRequest can be generate using layerize:dto
public function update(UpdateUserRequest $request, string $id)
{
try {
// Render from use case
// DB::beginTransaction();
// $this->userUseCase->execUpdate($request, $id);
// DB::commit();
// return redirect()->back()->with('success', "Data Successfully Updated");
} catch(Throwable $th) {
// DB::rollBack();
// return redirect()->back()->with('error', $th->getMessage());
}
}
public function delete(string $id)
{
try {
// Render from use case
// DB::beginTransaction();
// $this->userUseCase->execDelete($request);
// DB::commit();
// return redirect()->back()->with('success', "Data Successfully Delete");
} catch(Throwable $th) {
// DB::rollBack();
// return redirect()->back()->with('error', $th->getMessage());
}
}
}
php artisan layerize:usecase UserUseCase
namespace App\Http\UseCase;
use Illuminate\Http\Request;
class UserUseCase
{
// Use case code with example implementation
public function __construct(
// protected UserQuery $userQuery,
// protected UserCommand $userCommand,
// protected UserDatatable $userDatatable,
) {
}
public function renderIndex()
{
// render view index
// return view('index');
}
public function renderDatatable(Request $request)
{
// render datatable
// return $this->userDatatable->datatable($request);
}
public function renderCreate()
{
// render view create
// return view('create');
}
// StoreUserRequest can be generate using layerize:dto, but must same with request send by controller
public function execStore(StoreUserRequest $request)
{
// exec store data
// return $this->userCommand->store($request),
}
public function renderEdit(string $id)
{
// render view create
// $findId = $this->userQuery->firstOrFail(['id' => $id]);
// return view('edit', compact('findId'));
}
// UpdateUserRequest can be generate using layerize:dto, but must same with request send by controller
public function execUpdate(UpdateUserRequest $request, string $id)
{
// exec store data
// return $this->userCommand->update($request, $id),
}
public function execDelete(string $id)
{
// exec store data
// return $this->userCommand->delete($id),
}
}
php artisan layerize:service UserPage/User --all
namespace App\Services\UserPage\User;
use App\Services\Service;
class UserQuery extends Service
{
public function __construct()
{
// self::setModel(User::class); call your model class name to use general function list
}
// Other Query Service here
public function getCustomQuery(string $id)
{
// Other code here if query not support in getByAttr method
}
}
namespace App\Services\UserPage\User;
use App\Services\Service;
class UserCommand extends Service
{
// Command Service here
// StoreUserRequest can be generate by layerize:dto, but must same with request send by UseCase
public function store(StoreUserRequest $request)
{
// Code for store data
}
// UpdateUserRequest can be generate by layerize:dto, but must same with request send by UseCase
public function update(UpdateUserRequest $request, string $id)
{
// Code for update data
}
public function delete(string $id)
{
// Code for delete data
}
}
namespace App\Services\UserPage\User;
use App\Services\Service;
class UserDatatable extends Service
{
// Datatable Service here
public function datatable(Request $request)
{
// Datatable server side processing code here
}
}
php artisan layerize:service UserPage/ApiProvider
namespace App\Services\UserPage;
use App\Services\Service;
class ApiProvider extends Service
{
// Your custom service code here
}
...
class UserQuery extends Service
{
public function __construct()
{
self::setModel(User::class); // add like this
}
...
}
...
class UserUseCase
{
public function __construct(
protected UserQuery $userQuery
)
{
}
...
}
$this->userQuery->chunk(1000, function ($items) {
foreach ($items as $item) {
// Lakukan sesuatu dengan setiap item
}
}, ['category' => 'books'], ['author'], 'title', 'desc');