l0n3ly/laravel-dynamic-helpers

Laravel Dynamic Helpers is a package that dynamically resolves helper classes and provides an Artisan command to generate custom helpers in your app/Helpers directory, making helper management effortless and fully automated.

Maintainers

Package info

github.com/idehen-divine/laravel-dynamic-helpers

pkg:composer/l0n3ly/laravel-dynamic-helpers

Statistics

Installs: 240

Dependents: 0

Suggesters: 0

Stars: 4

Open Issues: 0

v1.3.0 2026-03-29 11:10 UTC

This package is auto-updated.

Last update: 2026-03-29 11:20:11 UTC


README

Latest Version Total Downloads License

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.

โœจ Features

  • ๐Ÿš€ Dynamic Helper Resolution - Automatically resolves helper classes on-demand
  • ๐ŸŽฏ Artisan Command Generator - Create helpers with php artisan make:helper
  • ๐Ÿ“ Nested Helper Support - Organize helpers in subdirectories (e.g., Store/CreateHelper)
  • ๐Ÿ”„ Singleton Pattern - Efficient instance caching for better performance
  • ๐ŸŽจ Laravel-Style Output - Beautiful command output matching Laravel's conventions
  • ๐Ÿ”Œ Auto-Discovery - Service provider automatically registered
  • ๐Ÿ’ก Dual Access Patterns - Use moneyHelper() or helpers()->moneyHelper()
  • ๐Ÿง  IDE Autocompletion - Generate IDE helper file with php artisan helpers:ide for full method autocomplete

๐Ÿ“‹ Requirements

  • PHP >= 8.1
  • Laravel >= 11.0 (11, 12, 13)

๐Ÿ“ฆ Installation

Install the package via Composer:

composer require l0n3ly/laravel-dynamic-helpers

The service provider will be automatically discovered by Laravel.

๐Ÿš€ Quick Start

1. Create a Helper

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

2. Use Your Helper

You can access your helper in two ways:

// Direct global function (recommended)
moneyHelper()->format(1000); // "1,000.00"
moneyHelper()->toMinor(1500); // 150000

// Via helpers() function
helpers()->moneyHelper()->format(2000); // "2,000.00"

๐Ÿ“š Usage Examples

Basic Helper

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
}

Nested Helpers

Create organized helper structures:

php artisan make:helper Store/CreateHelper
php artisan make:helper Store/Product/UpdateHelper

This creates:

  • app/Helpers/Store/CreateHelper.php
  • app/Helpers/Store/Product/UpdateHelper.php

Access them using flattened camelCase:

// Store/CreateHelper -> storeCreateHelper()
storeCreateHelper()->create($data);

// Store/Product/UpdateHelper -> storeProductUpdateHelper()
storeProductUpdateHelper()->update($id, $data);

In Controllers

<?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
        }
    }
}

In Blade Templates

@if(permissionHelper()->can('view-reports'))
    <div class="reports">
        {{ moneyHelper()->format($total) }}
    </div>
@endif

๐ŸŽฏ Advanced Features

Instance Caching

Helpers are automatically cached as singletons:

$helper1 = moneyHelper();
$helper2 = moneyHelper();

// $helper1 and $helper2 are the same instance

Callable Helpers

Helpers can be callable:

class CalculatorHelper extends Helper
{
    public function __invoke($a, $b)
    {
        return $a + $b;
    }
}

Usage:

$result = calculatorHelper(5, 10); // 15

Custom Helper Methods

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

๐Ÿ’ก IDE Autocompletion

The package includes an IDE helper generator so your editor understands what helpers()->storeCreateHelper() returns and can autocomplete methods on it.

Generate the IDE helper file

php artisan helpers:ide

This creates _ide_helper_helpers.php in your project root. The file is automatically added to your .gitignore on first run.

Note: php artisan make:helper automatically regenerates this file for you. You only need to run it manually when you create or delete helper files without using the artisan command.

Keep it in sync automatically (recommended)

Add this to your app's composer.json so it also regenerates whenever you run composer install, composer update, or composer dump-autoload:

"scripts": {
    "post-autoload-dump": [
        "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
        "@php artisan package:discover --ansi",
        "@php artisan helpers:ide --ansi || true"
    ]
}

๐Ÿ“– Command Reference

Create a Helper

php artisan make:helper HelperName

Generate IDE Helper

php artisan helpers:ide

Custom output path:

php artisan helpers:ide --output=ide_helpers.php

Create Nested Helper

php artisan make:helper Category/ProductHelper
php artisan make:helper Admin/User/PermissionHelper

Helper Name Normalization

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

๐Ÿงช Testing

Run the test suite:

composer test

Or with PHPUnit:

vendor/bin/phpunit

๐Ÿ“ Code Style

This package uses Laravel Pint for code style. Format code:

./vendor/bin/pint

๐Ÿค Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

๐Ÿ“„ License

The MIT License (MIT). Please see the License File for more information.

๐Ÿ‘ค Author

Divine Idehen

๐Ÿ™ Acknowledgments

  • Inspired by Laravel's elegant architecture
  • Built with the Laravel community in mind

Made with โค๏ธ for the Laravel community