Skip to content

Usage via Facade or Helper Class

Toni Suárez Rios edited this page May 20, 2025 · 2 revisions

Facades vs. Helpers – Pick Your Style!

So, you're wrangling UTM parameters in your Laravel app and want a clean way to access them? The Laravel UTM-Parameters package (we've been talking about its use cases!) offers a couple of neat approaches: using its Facade or a set of handy Helper functions. Both get you to the same place, so it really boils down to your personal preference and how you like to structure your code.

Let's break down these two options.

Option 1: The Facade Approach

If you're comfortable with Laravel's Facades, this will feel right at home. Facades provide a static-like interface to services in Laravel's service container, making them easy to call from anywhere in your application – controllers, views, middleware, you name it.

Here’s how you might use the UtmParameter Facade:

Imagine you want to check if a user landed on a page with utm_campaign=special-sale and redirect them if they did:

use Suarez\UtmParameter\Facades\UtmParameter; // Don't forget to import it!

// Somewhere in your controller or middleware...
if (UtmParameter::has('campaign', 'special-sale')) {
    // Yep, the 'campaign' UTM is 'special-sale'
    return redirect('/special-offer-page');
}

// If you just want to check if a UTM key exists, regardless of its value:
if (UtmParameter::has('source')) {
    // The URL has a utm_source parameter
    $sourceValue = UtmParameter::get('source'); // You can also get its value
    // Log::info('Visitor from source: ' . $sourceValue);
}

What's happening here?

  • UtmParameter::has('campaign', 'special-sale'): This checks if a UTM parameter named campaign exists and if its value is special-sale.
  • UtmParameter::has('source'): This simply checks for the presence of a utm_source parameter.
  • UtmParameter::get('source'): If you need the actual value of a UTM parameter, get() is your friend.

It's clean, expressive, and fits well into the typical Laravel way of doing things.

Option 2: The Helper Function Approach

Prefer a more global, function-based style? The package also provides helper functions that you can call directly, often leading to more concise code, especially in views or simpler route closures.

Let's achieve the same special-sale redirect using a helper:

// No need for a 'use' statement for global helpers

// In your controller, route, or even a Blade template (with caution for logic in views)
if (has_utm('campaign', 'special-sale')) {
    // Just like the Facade, this checks key and value
    return redirect('/special-offer-page');
}

// Checking for existence:
if (has_utm('source')) {
    $sourceValue = get_utm('source'); // And getting the value
    // Do something with $sourceValue
}

The lowdown:

  • has_utm('campaign', 'special-sale'): This helper does the exact same thing as UtmParameter::has('campaign', 'special-sale').
  • has_utm('source'): Checks if the utm_source key is present.
  • get_utm('source'): Fetches the value of the utm_source parameter.

Helper functions can make your code look a bit more streamlined in certain contexts, and they're globally available without needing an import.

So, Facade or Helper? Your Call!

Both methods are effective and provide convenient ways to interact with UTM parameters using the Laravel UTM-Parameters package.

  • Choose Facades if: You prefer the explicit, object-oriented feel of Laravel's Facades, appreciate the auto-completion in IDEs that often comes with them, or are working within classes where importing the Facade is standard practice.
  • Choose Helper Functions if: You value conciseness, want to quickly use the functionality in various places (including Blade templates, though keep complex logic out of views!), or prefer a more functional programming style.

Ultimately, the best approach is the one that fits your team's coding standards and your personal preference. The good news is, this package gives you the flexibility to choose!

Clone this wiki locally