Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Stringer Laravel Package

avris/stringer

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require avris/stringer
    

    No configuration is required—just autoload the package.

  2. 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!');
    
  3. Key Features to Explore:

    • Slug Generation: Stringer::slug('String')
    • String Truncation: Stringer::truncate('Long string', 10)
    • String Formatting: Stringer::titleize('hello_world')
    • Character Counting: Stringer::count('Hello', 'words')
  4. Where to Look First:

    • Facade API Docs (if available).
    • Helper Functions for quick reference.
    • Run php artisan vendor:publish --provider="Avris\Stringer\StringerServiceProvider" to publish config (if needed).

Implementation Patterns

Common Workflows

  1. Slug Generation for URLs:

    // In a controller or service
    $postSlug = Stringer::slug($request->title);
    $post->slug = $postSlug;
    $post->save();
    
  2. Dynamic String Formatting in Blade:

    <h1>{{ Stringer::titleize($post->title) }}</h1>
    <p>{{ Stringer::truncate($post->content, 200) }}</p>
    
  3. Validation Rules:

    use Avris\Stringer\Rules\Slug;
    
    $rules = [
        'title' => ['required', new Slug],
    ];
    
  4. Batch Processing:

    $posts = Post::all();
    $posts->each(function ($post) {
        $post->update(['slug' => Stringer::slug($post->title)]);
    });
    
  5. Customizing Output:

    // Override default slug separator
    Stringer::setSlugSeparator('-');
    
    // Or configure globally in `config/stringer.php`
    

Integration Tips

  • Laravel Filters: Use Stringer in app/Http/Middleware for request sanitization.
    $request->merge([
        'slug' => Stringer::slug($request->title),
    ]);
    
  • API Responses: Format strings before returning JSON:
    return response()->json([
        'title' => Stringer::titleize($request->title),
    ]);
    
  • Testing: Mock the facade for isolated tests:
    $this->mock(Avris\Stringer\Facades\Stringer::class)->shouldReceive('slug')->andReturn('test-slug');
    

Gotchas and Tips

Pitfalls

  1. Locale Sensitivity:

    • Some methods (e.g., slug) may not handle non-Latin characters well by default.
    • Fix: Configure config/stringer.php to use a custom transliterator:
      'transliterator' => \Symfony\Component\String\UnicodeString::class,
      
  2. Helper vs. Facade:

    • The stringer() helper may not be autoloaded by default.
    • Fix: Ensure Avris\Stringer\helpers is included in composer.json autoload or manually require the helper file.
  3. Performance:

    • Avoid chaining heavy methods (e.g., slug + titleize) in loops.
    • Tip: Cache results if processing large datasets:
      $slug = cache()->remember("slug_{$post->id}", now()->addHours(1), fn() => Stringer::slug($post->title));
      
  4. Edge Cases:

    • Empty strings or null inputs may throw errors in some methods.
    • Tip: Use null-safe calls:
      $slug = Stringer::slug($request->title ?? '');
      

Debugging

  • Check Config: Run php artisan config:clear if methods behave unexpectedly.
  • Log Outputs:
    \Log::debug('Slug:', ['input' => $string, 'output' => Stringer::slug($string)]);
    
  • Inspect Source: The package is minimal; review src/Stringer.php for custom logic.

Extension Points

  1. 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);
    
  2. 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;
        }
    }
    
  3. Override Helpers: Publish and modify the helper file:

    php artisan vendor:publish --tag=stringer-helpers
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
abbasudo/artisan-route
mirvan/ippanel
ignibyte/aic-framework
glavweb/data-schema-bundle
glavweb/datagrid-bundle
tacticmedia/scim-server
uni-method/frame
neika-ai/sdk
adachsoft/php-code-style
pharaonic/laravel-notification-dependencies
beautinow/translug
dev-brainiac/bot-cloaked
octoberrain/html
octoberrain/extension
octoberrain/exception
octoberrain/contracts
adachsoft/api-version-bundle
cgv/localized-resource
noith/settings
kda/laravel-package