kaue-f / laravel-structura
Laravel-Structura is a tool designed to streamline and standardize the creation of resources in Laravel, promoting a more organized and scalable development structure.
Requires
- php: ^8.2
- illuminate/support: ^10.0|^11.0|^12.0|^13.0
Requires (Dev)
- laravel/pint: ^1.29
- orchestra/testbench: ^9.0|^10.0|^11.0
- phpunit/phpunit: ^11.0
README
English | π§π· PortuguΓͺs
Laravel Structura
Structura comes from Latin and means structure and organization, reflecting the package's purpose.
π Introduction
Laravel Structura is a Laravel package designed to simplify, standardize, and structure the creation of application resources, promoting a clean, scalable, and well-organized development environment.
Through custom Artisan commands, the package enables the automatic generation of classes such as Actions, Cache, DTOs, Enums, Helpers, Services and Traits, encouraging clear separation of responsibilities and solid architectural best practices.
The main goal of Structura is to reduce repetitive tasks, ensure structural consistency, and help developers keep Laravel projects well-organized as they grow.
β¨ Features
- β Action generation with Makeable & Transaction support
- β Cache generation with CacheSupport extension
- β DTO generation with readonly/final patterns
- β
Enum generation with PHP Attributes and
toData()mapping - β Helper generation with global autoload registration
- β Trait generation
- β Service generation with ServiceResult and Makeable support
- β Automatic namespace organization
- β Consistent architectural patterns
- β
Centralized configuration via the
config/structura.phpfile - β CLI options override default configuration
- β Automatic suffix enforcement per class type
π Requirements
- PHP ^8.2
- Laravel ^10.x | ^11.x | ^12.x
π¦ Installation
composer require kaue-f/laravel-structura --dev
βοΈ Publishing the configuration file
php artisan structura:install
php artisan structura:install --force # Force overwrite
This command creates a new structura.php file in the Laravel application's config directory. It controls namespaces, paths, suffixes, and default options for each generator.
π AI Integration (Laravel Boost)
This package ships with a built-in Laravel Boost skill. If your project uses Boost, the Structura skill is automatically discovered and installed when you run:
php artisan boost:install
Once installed, your AI agent will understand the Structura architecture β Actions, Services, DTOs, Enums, Caches, and Helpers β and follow all naming conventions and patterns automatically.
π Available commands
| Command | Description |
|---|---|
structura:action |
Create Action classes with Makeable & Transaction support |
structura:cache |
Create Cache classes with optional CacheSupport |
structura:dto |
Create Data Transfer Object (DTO) classes |
structura:enum |
Create Enum classes with PHP Attribute mapping |
structura:helper |
Create Helper classes or global helpers |
structura:service |
Create Service classes with Result & Makeable |
structura:trait |
Create Trait classes |
structura:install |
Publish Structura configuration file |
π Usage examples
Action
php artisan structura:action Logout php artisan structura:action Logout --execute # Default (-e): generates execute() method php artisan structura:action Logout --handle # (-l): generates handle() method php artisan structura:action Logout --invokable # (-i): generates __invoke() method php artisan structura:action Logout --construct # (-c): generates __construct() method php artisan structura:action Logout --makeable # (-m): attaches Makeable trait β LogoutAction::run() php artisan structura:action Logout --transaction # (-t): wraps method body in DB::transaction() php artisan structura:action Logout --raw # (-r): creates empty class body
Default method:
execute(). Override with--handle,--invokable, or--construct.Pro Tip: By default,
makeableistrueinconfig/structura.phpβ every new Action is ready forLogoutAction::run()usage out of the box!
Cache
php artisan structura:cache Classification php artisan structura:cache Classification --extend # (-e): extends CacheSupport, adds $prefix property php artisan structura:cache Classification --raw # (-r): standalone class without CacheSupport
Use
--extendto inherit helper methods fromCacheSupport(e.g.,remember(),forget()).--rawcreates a plain class.--extendand--raware mutually exclusive.
DTO
php artisan structura:dto User php artisan structura:dto User --no-final # Removes the final modifier php artisan structura:dto User --no-readonly # Removes the readonly modifier php artisan structura:dto User --no-construct # Removes the __construct method php artisan structura:dto User --trait # (-t): attaches InteractsWithDTO trait php artisan structura:dto User --raw # (-r): plain class, no modifiers or helpers
DTOs are
final readonlyby default following PHP 8.2 best practices. Attach--traitto unlockMyDTO::fromRequest($request)andMyDTO::fromArray($data)helpers.--rawcannot be combined with other flags.
Enum
php artisan structura:enum Status php artisan structura:enum Status --backed=string # Backed enum (string|int) php artisan structura:enum Status --backed=string --cases=ACTIVE,INACTIVE # Pre-generates cases php artisan structura:enum Status --label # (-l): adds #[Label] attribute to each case php artisan structura:enum Status --trait # (-t): attaches InteractsWithEnum trait
Modern Enum Usage: Use
toData()for powerful frontend integration:Status::toData(); // Minimalist: ['id' => '...', 'name' => '...'] Status::toData(color: true, icon: true); // Includes color and icon attributes Status::toData(map: ['value' => 'id', 'label' => 'name']); // Custom key renaming Status::toData(map: ['extra' => fn($case) => $case->extra()]); // Closure resolutionThe
InteractsWithEnumtrait addstryFromDefault()fallback support.#[Label],#[Color],#[Icon], and#[DefaultCase]Attributes are supported.
Helper
php artisan structura:helper StringHelper php artisan structura:helper StringHelper --example # (-e): generates example method php artisan structura:helper StringHelper --global # (-g): creates global helpers.php and auto-registers it in composer.json autoload.files + runs dump-autoload php artisan structura:helper --stub # (-s): creates helper from the package's own stub
The
--globalflag automatically updatescomposer.jsonand runscomposer dump-autoloadso your global functions are immediately available.
Service
php artisan structura:service Comment php artisan structura:service Comment --construct # (-c): adds __construct() method php artisan structura:service Comment --method=process # (--m): generates a custom named method php artisan structura:service Comment --result # (--res): method returns ServiceResult php artisan structura:service Comment --makeable # (--mk): attaches Makeable trait
ServiceResult standardizes your service responses:
return ServiceResult::success($data); return ServiceResult::failure('Error message');Smart Generation: Combining
--method=processwith--makeableautomatically injectsprotected string $makeableMethod = 'process'soCommentService::run()dispatches to the right method instantly.
Trait
php artisan structura:trait Loggable
Traits do not receive automatic suffixes (unlike Actions, Services, etc.). So
structura:trait LoggablegeneratesLoggable.php, notLoggableTrait.php.
βοΈ Default Configuration (config/structura.php)
After publishing, you can set package-wide defaults in config/structura.php:
'default_options' => [ 'action' => [ 'execute' => true, // Default method 'makeable' => true, // All new actions get Makeable trait by default 'transaction' => false, ], 'service' => [ 'makeable' => false, 'result' => false, ], 'dto' => [ 'no-final' => false, 'no-readonly' => false, ], 'enum' => [ 'backed' => 'string', // Default backing type: 'string' | 'int' | null ], // ... ],
CLI flags always override config defaults.
π§± Example Structure
app/
βββ Actions/
β βββ LogoutAction.php
β
βββ Caches/
β βββ ClassificationCache.php
β
βββ Concerns/
β βββ Loggable.php
β
βββ DTOs/
β βββ UserDTO.php
β
βββ Enums/
β βββ StatusEnum.php
β
βββ Helpers/
β βββ helpers.php
β βββ StringHelper.php
β βββ string_helper.php
β
βββ Services/
β βββ CommentService.php
π License
Released under the MIT License.