l0n3ly/laravel-dynamic-helpers
A powerful Laravel package that provides a dynamic helper management system with an Artisan command generator. Create, organize, and access your custom helper classes effortlessly.
php artisan make:helperStore/CreateHelper)moneyHelper() for real global functions with IDE autocompleteInstall the package via Composer:
composer require l0n3ly/laravel-dynamic-helpers
The service provider will be automatically discovered by Laravel.
php artisan make:helper MoneyHelper
This creates app/Helpers/MoneyHelper.php:
<?php
namespace App\Helpers;
use L0n3ly\LaravelDynamicHelpers\Helper;
class MoneyHelper extends Helper
{
public function format($amount)
{
return number_format($amount, 2);
}
public function toMinor($amount)
{
return $amount * 100;
}
}
The package automatically registers global functions for all your helpers at boot time. You can access them directly:
// Direct global function (automatic, with IDE autocomplete)
moneyHelper()->format(1000); // "1,000.00"
moneyHelper()->toMinor(1500); // 150000
You can also access helpers through the helpers() function if preferred:
// Via helpers() function
helpers()->moneyHelper()->format(2000); // "2,000.00"
php artisan make:helper PermissionHelper
<?php
namespace App\Helpers;
use L0n3ly\LaravelDynamicHelpers\Helper;
class PermissionHelper extends Helper
{
public function can($permission)
{
return auth()->user()->hasPermission($permission);
}
}
Usage:
if (permissionHelper()->can('edit-posts')) {
// User can edit posts
}
Create organized helper structures:
php artisan make:helper Store/CreateHelper
php artisan make:helper Store/Product/UpdateHelper
This creates:
app/Helpers/Store/CreateHelper.phpapp/Helpers/Store/Product/UpdateHelper.phpAccess them using flattened camelCase:
// Store/CreateHelper -> storeCreateHelper()
storeCreateHelper()->create($data);
// Store/Product/UpdateHelper -> storeProductUpdateHelper()
storeProductUpdateHelper()->update($id, $data);
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
class OrderController extends Controller
{
public function store(Request $request)
{
$amount = moneyHelper()->toMinor($request->amount);
if (permissionHelper()->can('create-orders')) {
// Create order
}
}
}
@if(permissionHelper()->can('view-reports'))
<div class="reports">
{{ moneyHelper()->format($total) }}
</div>
@endif
Helpers are automatically cached as singletons:
$helper1 = moneyHelper();
$helper2 = moneyHelper();
// $helper1 and $helper2 are the same instance
Helpers can be callable:
class CalculatorHelper extends Helper
{
public function __invoke($a, $b)
{
return $a + $b;
}
}
Usage:
$result = calculatorHelper(5, 10); // 15
Add any methods you need:
class ApiHelper extends Helper
{
public function get($url)
{
return Http::get($url);
}
public function post($url, $data)
{
return Http::post($url, $data);
}
}
php artisan make:helper HelperName
php artisan make:helper Category/ProductHelper
php artisan make:helper Admin/User/PermissionHelper
The command automatically normalizes names:
# All of these create "MoneyHelper"
php artisan make:helper MoneyHelper
php artisan make:helper money-helper
php artisan make:helper money_helper
Run the test suite:
composer test
Or with PHPUnit:
vendor/bin/phpunit
This package uses Laravel Pint for code style. Format code:
./vendor/bin/pint
Contributions are welcome! Please feel free to submit a Pull Request.
The MIT License (MIT). Please see the License File for more information.
Divine Idehen
Made with โค๏ธ for the Laravel community
How can I help you explore Laravel packages today?