1. Go to this page and download the library: Download mehedi8gb/api-crudify 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/ */
mehedi8gb / api-crudify example snippets
final class ProductController extends Controller
{
public function __construct(
private readonly ProductService $service
) {}
public function index(): JsonResponse
{
$collection = $this->service->getProductsCollection();
return $this->successResponse('Products retrieved successfully', $collection);
}
}
class ProductService extends BaseService
{
public function __construct(
protected ProductRepository $productRepository,
protected Request $request
) {
parent::__construct($productRepository, $request);
}
public function getProductsCollection(): array
{
$data = $this->productRepository->getProductsData();
return $this->prepareResourceResponse($data, ProductResource::class);
}
}
class ProductRepository extends BaseRepository
{
public function __construct(Product $model, protected Request $request)
{
parent::__construct($model, $request);
}
public function getProductsData(array $with = ['category', 'tags']): array
{
return $this->handleApiQueryRequest($this->query(), $with);
}
}