Installation:
composer require dontdrinkandroot/utils-bundle
Add to config/bundles.php (Symfony) or config/app.php (Laravel via Symfony bridge):
DontDrinkAndRoot\UtilsBundle\DontDrinkAndRootUtilsBundle::class => ['all' => true],
First Use Case:
Check the Utils service for quick utilities like:
$this->get('utils')->slugify('Hello World'); // Returns 'hello-world'
Verify via TWIG (if enabled):
{{ app('utils').slugify('Test String') }}
Key Classes:
Utils (core utilities)ArrayUtils (array manipulations)StringUtils (string helpers)String Manipulation:
// Slugify, truncate, or format strings
$slug = $this->get('utils')->slugify($title);
$shortText = $this->get('utils')->truncate($longText, 50);
Array Operations:
// Merge, diff, or flatten arrays
$merged = $this->get('array_utils')->mergeRecursive($array1, $array2);
$diff = $this->get('array_utils')->arrayDiffAssoc($array1, $array2);
Dependency Injection:
// Inject services in controllers/services
public function __construct(Utils $utils) {
$this->utils = $utils;
}
Twig Integration:
{# Filter strings in templates #}
{{ 'Hello World'|slugify }}
app('utils') or bind the service in AppServiceProvider:
public function register() {
$this->app->bind('utils', function() {
return new \DontDrinkAndRoot\UtilsBundle\Utils();
});
}
FormRequest with custom rules:
use DontDrinkAndRoot\UtilsBundle\Utils;
public function rules() {
return [
'title' => ['required', function($attribute, $value, $fail) {
if (strlen($this->utils->slugify($value)) > 50) {
$fail('Slug too long.');
}
}]
];
}
Outdated Codebase:
slugify() may not handle non-ASCII characters well.No Laravel Native Support:
Undocumented Methods:
StringUtils::camelCase()) lack examples. Check source for usage:
$this->get('string_utils')->camelCase('hello_world'); // 'helloWorld'
Service Not Found:
Ensure the bundle is registered in config/bundles.php and dependencies are loaded.
php bin/console debug:container utils
Performance: Avoid heavy operations (e.g., recursive array merges) in loops. Cache results if needed:
$cachedSlug = Cache::remember("slug_{$title}", 3600, function() use ($title) {
return $this->utils->slugify($title);
});
Custom Utilities:
Extend the Utils class or create a decorator:
class CustomUtils extends \DontDrinkAndRoot\UtilsBundle\Utils {
public function customMethod($input) { ... }
}
Register the new service in services.yaml (Symfony) or AppServiceProvider.
Override Defaults:
Replace the bundle’s services in config/services.yaml:
services:
utils: '@app.custom_utils_service'
Testing:
Mock the Utils service in PHPUnit:
$this->mock(Utils::class)->shouldReceive('slugify')->andReturn('test-slug');
How can I help you explore Laravel packages today?