Weave Code
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

Dna Exchange Bundle Laravel Package

dnaklik/dna-exchange-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Install the package via Composer:

composer require vendor/package-name

Publish the configuration (if applicable) and run migrations:

php artisan vendor:package publish --provider="Vendor\Package\ServiceProvider"
php artisan migrate

The package now provides two key methods for working with slug-based content retrieval:

  • getContentFromSlug($slug) – Fetch a content collection by slug.
  • getRelatedContentFromContent($slug, $max) – Retrieve related content for a given slug (limited to $max results).

First Use Case:

// Fetch a content collection by slug
$content = app('content-service')->getContentFromSlug('example-slug');

// Get related content (e.g., top 5)
$related = app('content-service')->getRelatedContentFromContent('example-slug', 5);

Check the config/package.php for default settings (e.g., related content logic, caching behavior).


Implementation Patterns

Core Workflows

  1. Slug-Based Retrieval Use getContentFromSlug() for direct content access (e.g., in route controllers or Blade templates):

    public function show(ContentService $service, $slug) {
        $content = $service->getContentFromSlug($slug);
        return view('content.show', compact('content'));
    }
    
  2. Related Content Attach related content to views or APIs:

    $related = $service->getRelatedContentFromContent($slug, 3);
    return response()->json(['content' => $content, 'related' => $related]);
    
  3. Caching Leverage Laravel’s cache (e.g., Redis) for slug-based queries:

    $content = Cache::remember("content_{$slug}", now()->addHours(1), function() use ($slug) {
        return $service->getContentFromSlug($slug);
    });
    

Integration Tips

  • Service Provider Binding Bind the service in AppServiceProvider for cleaner dependency injection:

    $this->app->bind('content-service', function($app) {
        return new \Vendor\Package\Services\ContentService();
    });
    
  • Route Model Binding Extend Laravel’s binding to resolve slugs:

    Route::bind('content', function($slug) {
        return app('content-service')->getContentFromSlug($slug);
    });
    
  • Eloquent Relationships If using Eloquent, alias methods for consistency:

    class Content extends Model {
        public function related() {
            return $this->hasManyThrough(RelatedModel::class, RelatedPivot::class)
                        ->limit(app('content-service')->getRelatedLimit());
        }
    }
    

Gotchas and Tips

Pitfalls

  1. Slug Uniqueness Ensure slugs are unique in your database to avoid conflicts in getContentFromSlug(). Add a database unique constraint if missing.

  2. Performance with getRelatedContentFromContent() The max parameter defaults to null (unlimited). Set a reasonable limit (e.g., 5) to avoid N+1 queries or excessive data loading.

  3. Caching Invalidation Manually clear related content caches when content is updated:

    Cache::forget("related_{$slug}");
    

Debugging

  • Missing Slugs Verify slugs are generated correctly (e.g., via Str::slug()) and stored in the database. Use:

    dd(app('content-service')->getContentFromSlug('nonexistent-slug')); // Returns null or empty collection.
    
  • Related Content Logic Override the default related content algorithm by extending the service:

    class CustomContentService extends \Vendor\Package\Services\ContentService {
        public function getRelatedContentFromContent($slug, $max) {
            // Custom logic (e.g., tag-based or distance-based)
        }
    }
    

Extension Points

  1. Custom Related Content Logic Override the getRelatedContentFromContent() method in a child class or use a strategy pattern:

    $service->setRelatedContentStrategy(new TagBasedStrategy());
    
  2. Slug Normalization Extend slug handling (e.g., locale support) by modifying the underlying query:

    $service->setSlugNormalizer(function($slug) {
        return Str::lower(trans($slug));
    });
    
  3. Event Hooks Listen for content updates to refresh caches or triggers:

    Content::updated(function($content) {
        Cache::forget("content_{$content->slug}");
    });
    
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.
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours