PHP code example of laraneat / modules

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);

// Find a module
$module = $repository->find('app/blog');

// Get module properties
$module->getName();           // "blog"
$module->getStudlyName();     // "Blog"
$module->getNamespace();      // "Modules\Blog"
$module->getPath();           // "/path/to/modules/blog"
$module->getProviders();      // ["Modules\Blog\Providers\BlogServiceProvider"]

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();
    }
}

// routes/v1.php
Route::post('/posts', CreatePostAction::class);
Route::get('/posts', ListPostsAction::class);
Route::get('/posts/{post}', ViewPostAction::class);
Route::put('/posts/{post}', UpdatePostAction::class);
Route::delete('/posts/{post}', DeletePostAction::class);

// 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,
        );
    }
}

// src/Providers/BlogServiceProvider.php
namespace Modules\Blog\Providers;

use Laraneat\Modules\Support\ModuleServiceProvider;

class BlogServiceProvider extends ModuleServiceProvider
{
    public function boot(): void
    {
        // Load module commands
        $this->loadCommandsFrom([
            'Modules\\Blog\\UI\\CLI\\Commands' => __DIR__ . '/../UI/CLI/Commands',
        ]);

        // Load migrations
        $this->loadMigrationsFrom(__DIR__ . '/../../database/migrations');

        // Load views
        $this->loadViewsFrom(__DIR__ . '/../../resources/views', 'blog');
    }
}

return [
    // Where modules are stored
    'path' => base_path('modules'),

    // Base namespace for all modules
    'namespace' => 'Modules',

    // Custom stubs location (optional)
    'custom_stubs' => base_path('stubs/modules'),

    // Composer settings for generated modules
    'composer' => [
        'vendor' => 'app',
        'author' => [
            'name' => 'Your Name',
            'email' => '[email protected]',
        ],
    ],

    // Component path/namespace mappings
    'components' => [
        'action' => [
            'path' => 'src/Actions',
            'namespace' => 'Actions',
        ],
        'model' => [
            'path' => 'src/Models',
            'namespace' => 'Models',
        ],
        // ... more components
    ],

    // Enable manifest caching (recommended for production)
    'cache' => [
        'enabled' => env('APP_ENV') === 'production',
    ],
];

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,
        ];
    }
}

// config/modules.php
'cache' => [
    'enabled' => env('APP_ENV') === 'production',
],
bash
php artisan module:cache
bash
php artisan vendor:publish --provider="Laraneat\Modules\Providers\ModulesServiceProvider"
bash
php artisan module:make Blog
bash
php artisan module:migrate

routes/
├── v1.php    # Version 1 routes
└── v2.php    # Version 2 routes
bash
php artisan module:cache