PHP code example of henrygodev / laranest

1. Go to this page and download the library: Download henrygodev/laranest 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/ */

    

henrygodev / laranest example snippets


class ProductController extends Controller
{
    public function index(): JsonResponse { ... }
    public function store(StoreProductRequest $request): JsonResponse { ... }
    public function show(Product $product): JsonResponse { ... }
    public function update(UpdateProductRequest $request, Product $product): JsonResponse { ... }
    public function destroy(Product $product): JsonResponse { ... }
}

class ProductController extends Controller
{
    public function index() { ... }
    public function create() { ... }
    public function store(StoreProductRequest $request) { ... }
    public function show(Product $model) { ... }
    public function edit(Product $model) { ... }
    public function update(UpdateProductRequest $request, Product $model) { ... }
    public function destroy(Product $model) { ... }
}

class ProductService
{
    public function getAll(): mixed { ... }
    public function findById(int $id): Product { ... }
    public function create(array $data): Product { ... }
    public function update(int $id, array $data): Product { ... }
    public function delete(int $id): void { ... }
}

class ProductController extends Controller
{
    public function __construct(protected ProductService $service) {}

    public function index(): JsonResponse
    {
        return response()->json($this->service->getAll());
    }

    public function store(StoreProductRequest $request): JsonResponse
    {
        return response()->json($this->service->create($request->validated()), 201);
    }
    // ...
}

return [
    'modules_path'      => 'Modules',       // app/Modules/
    'modules_namespace' => 'App\\Modules',

    'structure' => [
        'models' => [
            'path'      => 'Models',
            'namespace' => 'Models',
            'generator' => \Henrygodev\LaravelModule\Generators\ModelGenerator::class,
            'stubs'     => [
                ['stub' => 'model.stub', 'prefix' => null, 'suffix' => null],
            ],
        ],
        // controllers, requests...
    ],
];

'modules_path'      => 'Domain',
'modules_namespace' => 'App\\Domain',

'structure' => [
    'models' => [
        'path'      => 'Domain/Entities',   // app/Modules/Products/Domain/Entities/
        'namespace' => 'Domain/Entities',   // App\Modules\Products\Domain\Entities
        'generator' => \Henrygodev\LaravelModule\Generators\ModelGenerator::class,
        'stubs'     => [
            ['stub' => 'model.stub', 'prefix' => null, 'suffix' => null],
        ],
    ],

    'requests' => [
        'path'      => 'Http/Requests',
        'namespace' => 'Http/Requests',
        'generator' => \Henrygodev\LaravelModule\Generators\RequestGenerator::class,
        'stubs'     => [
            ['stub' => 'store-request.stub',  'prefix' => 'Store',  'suffix' => 'Request'],
            ['stub' => 'update-request.stub', 'prefix' => 'Update', 'suffix' => 'Request'],
            ['stub' => 'index-request.stub',  'prefix' => 'Index',  'suffix' => 'Request'], // custom
        ],
    ],
],

// app/Generators/RepositoryGenerator.php
class RepositoryGenerator extends \Henrygodev\LaravelModule\Generators\BaseGenerator
{
    public function generate(): void
    {
        $this->command->info("Creating repository {$this->context->name}");
        $this->generateFromConfig();
    }
}

'structure' => [
    // ...existing entries...

    'repositories' => [
        'path'      => 'Repositories',
        'namespace' => 'Repositories',
        'generator' => \App\Generators\RepositoryGenerator::class,
        'stubs'     => [
            ['stub' => 'repository.stub', 'prefix' => null, 'suffix' => 'Repository'],
        ],
    ],
],

// stubs/laravel-module/model.stub

 
namespace {{ namespace }};
 
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
 
class {{ class }} extends Model
{
    use SoftDeletes;
 
    protected $guarded = [];
}
bash
php artisan make:module Product

app/
└── Modules/
    └── Products/
        ├── Controllers/
        │   └── ProductController.php
        ├── Models/
        │   └── Product.php
        └── Requests/
            ├── StoreProductRequest.php
            └── UpdateProductRequest.php

database/
    └── migrations/
        └── 2025_01_01_000000_create_products_table.php

app/
└── Modules/
    └── Products/
        └── Services/
            └── ProductService.php
bash
php artisan make:module ProductCategory
# or
php artisan make:module product_category