larapi/zkrcrud

A Laravel package to simplify the creation of RESTful APIs, similar to Orion.

Installs: 1

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

pkg:composer/larapi/zkrcrud

v1.0.0 2025-04-27 19:25 UTC

This package is not auto-updated.

Last update: 2025-12-25 10:34:41 UTC


README

Laravel PHP License

ZKRCrud is a powerful Laravel package that automates RESTful API development with built-in CRUD operations, advanced filtering, and policy-based authorization. Perfect for rapid API development with Laravel 10+.

✨ Key Features

  • 🚀 Auto-generated CRUD endpoints
  • 🔒 Policy-based authorization
  • 🔍 Advanced query filtering (Spatie Query Builder)
  • Automatic request validation
  • 📊 Smart pagination
  • 🛡️ Comprehensive error handling
  • 🔄 Pre/post operation hooks

📦 Installation

Install via Composer:

composer require larapi/zkrcrud

🚀 Quick Start
1. Create Controller
<?php 
namespace App\Http\Controllers;

use Larapi\Zkrcrud\Http\Controllers\ZkrController;
use App\Models\Product;

class ProductController extends ZkrController
{
    protected $model = Product::class;
    
    protected $allowedIncludes = ['category', 'reviews'];
    protected $allowedFilters = ['name', 'price', 'in_stock'];
    protected $allowedSorts = ['created_at', 'price'];
    protected $allowedFields = ['id', 'name', 'price'];
    
    protected function rules(): array
    {
        return [
            'name' => 'required|string|max:100',
            'price' => 'required|numeric|min:0',
        ];
    }
}
  1. Define Routes
<?php
use App\Http\Controllers\ProductController;

Route::apiResource('products', ProductController::class);

2. Define Routes

use App\Http\Controllers\ProductController;

Route::apiResource('products', ProductController::class);

🔍 API Endpoints

Method | Endpoint | Description GET | /products | Paginated product list GET | /products/{id} | Get single product POST | /products | Create new product PUT | /products/{id} | Update product DELETE | /products/{id} | Delete product

⚙️ Advanced Usage

Query Filtering

GET /products?filter[price][gt]=100&include=category&sort=-created_at

Supported Parameters:

include – Load relationships

filter – Filter by fields

sort – Sort results (- prefix for DESC)

fields – Select specific fields

per_page – Items per page (pagination)

🛠️ Custom Hooks Example usage of hooks inside the controller:

protected function beforeStore(Request $request)
{
    // Logic before creation
    $request->merge(['created_by' => auth()->id()]);
}

protected function afterUpdate($model, Request $request)
{
    // Logic after update
    $model->history()->create($request->all());
}

🔒 Authorization Automatically checks the following policy methods:

Action Policy Method List viewAny View view Create create Update update Delete delete

🛠️ Configuration Required:

<?php
class YourController extends Controller{
protected $model = YourModel::class;
protected ?string $requestClass = CustomRequest::class;
protected array $allowedIncludes = [];
protected array $allowedFilters = [];
protected array $allowedSorts = [];
protected array $allowedFields = [];
/// rest of the code
}

Optional:

protected ?string $requestClass = CustomRequest::class;
protected array $allowedIncludes = [];
protected array $allowedFilters = [];
protected array $allowedSorts = [];
protected array $allowedFields = [];

🛠️ System Requirements PHP 8.1+

Laravel 10+

Composer 2.0+

🤝 Contributing Pull requests are welcome! Please follow PSR-12 coding standards.

Code of Conduct

In order to ensure that the Laravel community is welcoming to all, please review and abide by the Code of Conduct.

Security Vulnerabilities

If you discover a security vulnerability within Laravel, please send an e-mail to Taylor Otwell via taylor@laravel.com. All security vulnerabilities will be promptly addressed.

License

The Laravel framework is open-sourced software licensed under the MIT license.