l0n3ly/laravel-dynamic-helpers
Installation:
composer require l0n3ly/laravel-dynamic-helpers
No additional configuration needed—auto-discovery handles registration.
First Helper Creation:
php artisan make:helper StringHelper
This generates app/Helpers/StringHelper.php with a base Helper class.
First Usage:
// Direct global function (automatically available)
StringHelper::capitalize('hello'); // "Hello"
Or via global function:
stringHelper()->capitalize('hello');
php artisan make:helper (for creating helpers)app/Helpers/ (where helpers are auto-discovered)L0n3ly\LaravelDynamicHelpers\DynamicHelpersServiceProvider (handles boot-time registration)Problem: Need reusable string manipulation logic across controllers/Blade. Solution:
php artisan make:helper StringHelper
Add methods to StringHelper:
public function truncate($string, $length = 50)
{
return Str::limit($string, $length);
}
Use anywhere:
stringHelper()->truncate($longText); // "Shortened text..."
Helper Creation:
php artisan make:helper [Nested/]HelperName
app/Helpers/[Nested/]HelperName.phpStore/ProductHelper → app/Helpers/Store/ProductHelper.php)Access Patterns (Choose One):
helperName() → stringHelper()->method()HelperName::method() (via __callStatic)helpers()->helperName()->method()Nested Helper Usage:
php artisan make:helper Auth/OAuthHelper
Access via camelCase:
authOAuthHelper()->getProvider();
public function show(Request $request)
{
$data = stringHelper()->sanitize($request->input('data'));
return view('view', ['data' => $data]);
}
<h1>{{ stringHelper()->titleCase($post->title) }}</h1>
$this->app->bind('stringHelper', function ($app) {
return new \App\Helpers\StringHelper();
});
Callable Helpers:
class CalculatorHelper extends Helper
{
public function __invoke($a, $b) { return $a + $b; }
}
Usage:
$sum = calculatorHelper(5, 10); // 15
Dependency Injection:
class StringHelper extends Helper
{
public function __construct(private Translator $translator) {}
}
Register binding in AppServiceProvider:
$this->app->bind(\App\Helpers\StringHelper::class, function ($app) {
return new \App\Helpers\StringHelper($app->make(Translator::class));
});
Grouped Helpers:
Organize helpers by domain (e.g., Auth/, Store/) and use flattened camelCase:
authOAuthHelper()->login(); // From `app/Helpers/Auth/OAuthHelper.php`
IDE Autocomplete Delays:
php artisan optimize:clear and restart your IDE.Naming Conflicts:
userHelper vs. auth()->user()).userManagementHelper.Singleton Behavior:
__construct() carefully to avoid shared state issues.Dynamic Method Calls:
BadMethodCallException.app/Helpers/ and the method is spelled correctly.Nested Helper Paths:
Store/ProductHelper requires app/Helpers/Store/).ClassNotFoundException if directories are missing.Check Registration:
dd(\L0n3ly\LaravelDynamicHelpers\Facades\Helpers::getHelpers());
Lists all registered helpers.
Verify Boot-Time Generation:
Check for generated IDE files in .idea/ or _ide_helper.php after boot.
Clear Cache:
php artisan optimize:clear
php artisan config:clear
app/Helpers/ recursively. Exclude files by prefixing with _ (e.g., _BaseHelper.php).config/helpers.php (if manually configured):
'helpers_path' => app_path('CustomHelpers'),
Custom Base Class:
Extend the base Helper class in app/Providers/AppServiceProvider:
use L0n3ly\LaravelDynamicHelpers\Helper as BaseHelper;
class CustomHelper extends BaseHelper
{
public function __construct()
{
// Custom logic
}
}
Then update your helpers to extend CustomHelper.
Dynamic Helper Registration:
Add custom helpers programmatically in AppServiceProvider@boot():
use L0n3ly\LaravelDynamicHelpers\Facades\Helpers;
Helpers::addHelper(new \App\Helpers\Custom\DynamicHelper());
IDE Helper Customization: Override the generated IDE files by publishing the template:
php artisan vendor:publish --tag=helpers-ide
Then modify config/helpers.php to point to your custom template.
private $cachedData;
public function expensiveOperation()
{
return $this->cachedData ??= $this->computeExpensiveData();
}
Service Container Binding: Bind helpers to the container for DI:
$this->app->bind(\App\Helpers\StringHelper::class, function ($app) {
return new \App\Helpers\StringHelper($app['cache']);
});
Then resolve via:
$helper = app(\App\Helpers\StringHelper::class);
Testing Helpers: Mock helpers in tests using partial mocks:
$helper = $this->partialMock(\App\Helpers\StringHelper::class, [], [], '', false);
$this->app->instance(\App\Helpers\StringHelper::class, $helper);
Helper Facade:
Use the Helpers facade for programmatic access:
use L0n3ly\LaravelDynamicHelpers\Facades\Helpers;
$helper = Helpers::get('stringHelper');
How can I help you explore Laravel packages today?