1. Go to this page and download the library: Download laraneat/modules 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/ */
laraneat / modules example snippets
// config/modules.php - Enable cache in production
'cache' => [
'enabled' => env('APP_ENV') === 'production',
],
class CreatePostAction
{
use AsAction;
// Business logic - can be called from anywhere
public function handle(CreatePostDTO $dto): Post
{
return Post::create($dto->all());
}
// HTTP entry point - acts as controller
public function asController(CreatePostRequest $request): JsonResponse
{
$post = $this->handle($request->toDTO());
return (new PostResource($post))->created();
}
}
use Laraneat\Modules\ModulesRepository;
$repository = app(ModulesRepository::class);
// Get all modules
$modules = $repository->getModules();
// Check if module exists
$repository->has('app/blog');
// Find module by name
$repository->filterByName('Blog');
// Delete a module
$repository->delete('app/blog');
// src/Actions/CreatePostAction.php
namespace Modules\Blog\Actions;
use Lorisleiva\Actions\Concerns\AsAction;
use Modules\Blog\DTO\CreatePostDTO;
use Modules\Blog\Models\Post;
use Modules\Blog\UI\API\Requests\CreatePostRequest;
use Modules\Blog\UI\API\Resources\PostResource;
use Illuminate\Http\JsonResponse;
class CreatePostAction
{
use AsAction;
// Core business logic - reusable from anywhere
public function handle(CreatePostDTO $dto): Post
{
return Post::create($dto->all());
}
// HTTP entry point - acts as controller
public function asController(CreatePostRequest $request): JsonResponse
{
$post = $this->handle($request->toDTO());
return (new PostResource($post))->created();
}
}
// src/DTO/CreatePostDTO.php
namespace Modules\Blog\DTO;
class CreatePostDTO
{
public function __construct(
public readonly string $title,
public readonly string $content,
public readonly int $authorId,
) {}
public static function fromRequest(CreatePostRequest $request): self
{
return new self(
title: $request->validated('title'),
content: $request->validated('content'),
authorId: $request->user()->id,
);
}
}
class CreatePostAction
{
use AsAction;
// Business logic - reusable, testable
public function handle(CreatePostDTO $dto): Post
{
$post = Post::create($dto->all());
event(new PostCreated($post));
return $post;
}
// HTTP concerns only - request/response handling
public function asController(CreatePostRequest $request): JsonResponse
{
$post = $this->handle($request->toDTO());
return (new PostResource($post))->created();
}
}
// From another Action
class ImportPostsAction
{
public function __construct(private CreatePostAction $createPost) {}
public function handle(array $posts): void
{
foreach ($posts as $postData) {
$this->createPost->handle(new CreatePostDTO(...$postData));
}
}
}
// From a Job
class ProcessImportJob implements ShouldQueue
{
public function handle(CreatePostAction $action): void
{
$action->handle($this->dto);
}
}
// From a Command
class SeedPostsCommand extends Command
{
public function handle(CreatePostAction $action): void
{
$action->handle(new CreatePostDTO(...));
}
}
class CreatePostDTO
{
public function __construct(
public readonly string $title,
public readonly string $content,
public readonly int $authorId,
) {}
public function all(): array
{
return [
'title' => $this->title,
'content' => $this->content,
'author_id' => $this->authorId,
];
}
}