Installation:
composer require avris/stringer
No configuration is required—just autoload the package.
First Use Case:
Use the Stringer facade or helper directly in a Blade view, controller, or service:
use Avris\Stringer\Facades\Stringer;
// Convert to slug
$slug = Stringer::slug('Hello World!');
// Or via helper (if enabled in `config/stringer.php`)
$slug = stringer('Hello World!');
Key Features to Explore:
Stringer::slug('String')Stringer::truncate('Long string', 10)Stringer::titleize('hello_world')Stringer::count('Hello', 'words')Where to Look First:
php artisan vendor:publish --provider="Avris\Stringer\StringerServiceProvider" to publish config (if needed).Slug Generation for URLs:
// In a controller or service
$postSlug = Stringer::slug($request->title);
$post->slug = $postSlug;
$post->save();
Dynamic String Formatting in Blade:
<h1>{{ Stringer::titleize($post->title) }}</h1>
<p>{{ Stringer::truncate($post->content, 200) }}</p>
Validation Rules:
use Avris\Stringer\Rules\Slug;
$rules = [
'title' => ['required', new Slug],
];
Batch Processing:
$posts = Post::all();
$posts->each(function ($post) {
$post->update(['slug' => Stringer::slug($post->title)]);
});
Customizing Output:
// Override default slug separator
Stringer::setSlugSeparator('-');
// Or configure globally in `config/stringer.php`
Stringer in app/Http/Middleware for request sanitization.
$request->merge([
'slug' => Stringer::slug($request->title),
]);
return response()->json([
'title' => Stringer::titleize($request->title),
]);
$this->mock(Avris\Stringer\Facades\Stringer::class)->shouldReceive('slug')->andReturn('test-slug');
Locale Sensitivity:
slug) may not handle non-Latin characters well by default.config/stringer.php to use a custom transliterator:
'transliterator' => \Symfony\Component\String\UnicodeString::class,
Helper vs. Facade:
stringer() helper may not be autoloaded by default.Avris\Stringer\helpers is included in composer.json autoload or manually require the helper file.Performance:
slug + titleize) in loops.$slug = cache()->remember("slug_{$post->id}", now()->addHours(1), fn() => Stringer::slug($post->title));
Edge Cases:
null inputs may throw errors in some methods.$slug = Stringer::slug($request->title ?? '');
php artisan config:clear if methods behave unexpectedly.\Log::debug('Slug:', ['input' => $string, 'output' => Stringer::slug($string)]);
Custom Methods:
Extend the Stringer class or create a decorator:
class CustomStringer extends \Avris\Stringer\Stringer {
public function customMethod($string) {
return strtoupper($string);
}
}
Bind it in a service provider:
$this->app->singleton(\Avris\Stringer\Contracts\Stringer::class, CustomStringer::class);
Add Rules:
Extend the Slug rule for custom validation:
use Avris\Stringer\Rules\Slug as BaseSlug;
class CustomSlug extends BaseSlug {
public function passes($attribute, $value) {
return parent::passes($attribute, $value) && strlen($value) > 3;
}
}
Override Helpers: Publish and modify the helper file:
php artisan vendor:publish --tag=stringer-helpers
How can I help you explore Laravel packages today?