sakshsky/laravel-global-helpers

Zero-config global helpers for Laravel. Just create `app/Helpers/helpers.php` and start using functions!

dev-main 2025-05-20 13:34 UTC

This package is not auto-updated.

Last update: 2025-06-04 12:03:48 UTC


README

# Sakshsky Laravel Global Helpers

[![Latest Version](https://img.shields.io/packagist/v/sakshsky/laravel-global-helpers.svg?style=flat-square)](https://packagist.org/packages/sakshsky/laravel-global-helpers)
[![Total Downloads](https://img.shields.io/packagist/dt/sakshsky/laravel-global-helpers.svg?style=flat-square)](https://packagist.org/packages/sakshsky/laravel-global-helpers)
[![License](https://img.shields.io/packagist/l/sakshsky/laravel-global-helpers.svg?style=flat-square)](https://github.com/sakshsky/laravel-global-helpers/blob/main/LICENSE)

A zero-configuration Laravel package to auto-load global helper functions. Just create `app/Helpers/helpers.php` and start using your functions everywhere—no manual `composer.json` edits required!

---

## Features
- 🚀 **Zero Configuration**: Auto-detects `helpers.php` in `app/Helpers/`.
- 📂 **Starter Template**: Optional pre-built `helpers.php` with common utilities.
- 🔧 **Artisan Command**: Quick setup with `php artisan sakshsky:install-helpers`.
- 💡 **IDE-Friendly**: Works seamlessly with PHPStorm/VSCode autocomplete.

---

## Installation

1. Install via Composer:
   ```bash
   composer require sakshsky/laravel-global-helpers
  1. (Optional) Publish the default helpers.php template:
    php artisan sakshsky:install-helpers
    This creates app/Helpers/helpers.php with example functions.

Usage

1. Create Your Helpers

Edit app/Helpers/helpers.php:

<?php
// Example: Custom slug generator
if (!function_exists('custom_slug')) {
    function custom_slug(string $text): string {
        return \Illuminate\Support\Str::slug($text) . '-' . time();
    }
}

// Example: Price formatter
if (!function_exists('format_price')) {
    function format_price(float $amount, string $currency = '$'): string {
        return $currency . number_format($amount, 2);
    }
}

2. Use Globally

Call functions anywhere—controllers, views, models, etc.:

// Controller
public function index() {
    $slug = custom_slug('Hello World'); // "hello-world-1712345678"
    $price = format_price(99.99);       // "$99.99"
}
<!-- Blade View -->
{{ format_price(49.99, '') }}  <!-- Output: "₹49.99" -->

Advanced Customization

1. Change Helpers Directory

Override the default path in AppServiceProvider:

public function boot() {
    $this->app->singleton('sakshsky.helpers.path', function () {
        return base_path('custom/path/helpers.php'); // Your custom path
    });
}

2. Add Preloaded Helpers (From Package)

Extend the HelpersServiceProvider to include default helpers:

public function register() {
    // Load user's helpers
    if (file_exists($userHelpers = app_path('Helpers/helpers.php'))) {
        require_once $userHelpers;
    }

    // Load package defaults (optional)
    require_once __DIR__.'/../helpers.php';
}

Example Helpers Template

The published helpers.php.stub includes these ready-to-use functions:

<?php
use Illuminate\Support\Str;

if (!function_exists('is_production')) {
    function is_production(): bool {
        return app()->environment('production');
    }
}

if (!function_exists('array_key_rename')) {
    function array_key_rename(array $array, string $oldKey, string $newKey): array {
        $array[$newKey] = $array[$oldKey];
        unset($array[$oldKey]);
        return $array;
    }
}

Publishing Updates

To update the helpers.php stub after modifications:

php artisan vendor:publish --tag=sakshsky-helpers --force

Testing

Run tests (if included in package):

composer test

Security

If you discover any security issues, please email security@sakshsky.com instead of creating a GitHub issue.

License

MIT © Sakshsky.
Free to use, modify, and distribute.