l0n3ly/laravel-dynamic-helpers
Installation:
composer require l0n3ly/laravel-dynamic-helpers
The package auto-discovers and registers its service provider.
Create your first helper:
php artisan make:helper MoneyHelper
This generates app/Helpers/MoneyHelper.php with a base Helper class.
Use immediately:
moneyHelper()->format(1000); // "1,000.00"
No manual registration needed—global functions are auto-generated at boot.
Scenario: Need reusable logic for formatting monetary values across controllers and Blade templates. Solution:
php artisan make:helper MoneyHelper
Edit the generated file:
class MoneyHelper extends Helper {
public function format($amount) {
return number_format($amount, 2);
}
}
Now use anywhere:
// Controller
$formatted = moneyHelper()->format($order->amount);
// Blade
{{ moneyHelper()->format($product->price) }}
Basic helpers:
php artisan make:helper StringHelper
Creates app/Helpers/StringHelper.php.
Nested helpers (for domain separation):
php artisan make:helper Store/ProductHelper
Creates app/Helpers/Store/ProductHelper.php and auto-generates storeProductHelper() global function.
Access patterns:
// Global function (preferred)
storeProductHelper()->getById(1);
// Static call
StoreProductHelper::getById(1);
// Proxy method
helpers()->storeProductHelper()->getById(1);
Service Providers: Inject helpers via constructor:
public function __construct(private MoneyHelper $moneyHelper) {}
Laravel’s DI resolves the singleton instance automatically.
Blade Directives: Create custom directives using helpers:
Blade::directive('currency', function ($amount) {
return "<?php echo moneyHelper()->format($amount); ?>";
});
Usage:
@currency($product->price)
Form Requests: Validate using helper logic:
public function rules() {
return [
'amount' => ['required', Rule::function(fn ($attr, $value) =>
moneyHelper()->isValidAmount($value)
)],
];
}
Unit Tests: Mock helpers in tests:
$this->app->instance(MoneyHelper::class, Mockery::mock(MoneyHelper::class));
Or use the global function:
$helper = $this->app->make(MoneyHelper::class);
$this->assertEquals('1,000.00', $helper->format(1000));
Feature Tests: Test Blade/Controller interactions:
$response = $this->get('/orders');
$response->assertSee('1,234.56'); // Formatted by moneyHelper()
Singleton Caching: Helpers are auto-cached as singletons. Avoid recreating instances:
// Good
$helper = moneyHelper();
// Bad (creates new instance)
$helper = new MoneyHelper();
Lazy-Loading: Helpers are only instantiated when first called, reducing boot time overhead.
Callable Helpers:
class CalculatorHelper extends Helper {
public function __invoke($a, $b) {
return $a + $b;
}
}
Usage:
$result = calculatorHelper(5, 10); // 15
Helper Composition: Combine helpers in a single class:
class OrderHelper extends Helper {
public function calculateTotal(Order $order) {
return moneyHelper()->format(
$order->items->sum(fn ($item) => $item->price * $item->quantity)
);
}
}
Namespace Conflicts:
authHelper).authenticationHelper.IDE Autocomplete Delays:
php artisan helpers:ide
_ide_helper.php from version control (it’s auto-generated).Helper Overwriting:
app/Helpers/Helper.php breaks the base class.Helper base class in app/Helpers/.Case Sensitivity in Nested Helpers:
Store/ProductHelper generates storeProductHelper(), not store_product_helper().PascalCase for helper names to avoid surprises.Dynamic Function Registration:
eval(). Avoid modifying them directly.bootstrap/cache/ for generated function files if helpers aren’t available.Helper Not Found?:
app/Helpers/ (case-sensitive).moneyHelper vs. MoneyHelper).IDE Not Recognizing Helpers:
php artisan helpers:ide to regenerate IDE files..phpstorm.meta.php is loaded (check Settings > PHP > Include Paths).Performance Issues:
tideways/xhprof to profile helper instantiation if boot time is slow.Custom Helper Directory:
app/Helpers/ directory, publish the config:
php artisan vendor:publish --tag=dynamic-helpers-config
config/dynamic-helpers.php:
'helpers_path' => app_path('CustomHelpers'),
Excluding Helpers:
config/dynamic-helpers.php:
'excluded_helpers' => [
'App\Helpers\Legacy\OldHelper',
],
Boot Order:
auth), ensure the provider boots first.Custom Base Helper:
Helper class by publishing the stub:
php artisan vendor:publish --tag=dynamic-helpers-stubs
stubs/helper.stub to add default methods or traits.Dynamic Helper Registration:
public function boot() {
$this->app->registerDynamicHelpers([
'App\Helpers\Custom\NamespaceHelper',
]);
}
IDE File Customization:
$this->app->bind(IdeHelperGenerator::class, CustomIdeHelperGenerator::class);
Helper Events:
event(new HelpersRegistered($helpers));
php artisan vendor:publish --tag=dynamic-helpers-events
Helper Naming Conventions:
Use PascalCase for helper classes and camelCase for global functions:
MoneyHelper → moneyHelper()Store/ProductHelper → storeProductHelper()Documentation: Add PHPDoc blocks to helpers for better IDE support:
/**
* Formats an amount as currency.
*
* @param float $amount
* @return string
*/
public function format($amount) { ... }
Testing Helpers:
Use the helpers() proxy in tests for better isolation:
$this->app->instance(MoneyHelper::class, Mockery::mock());
helpers()->moneyHelper()->shouldReceive('format')->andReturn('100.00');
Laravel Boost Integration: Leverage AI-powered helper scaffolding:
php artisan boost:skill:install l0n3ly/laravel-dynamic-helpers
Then use Boost to generate helpers with natural language:
php artisan boost:generate "create a helper for formatting dates"
How can I help you explore Laravel packages today?