how to create global function that can be accessed from any controller and blade file

how to create global function that can be accessed from any controller and blade file

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Creating Global Functions in Laravel Controllers and Blade Files

When you find yourself repeating logic across multiple controllers like HomeController and BackendController, or needing to access utility methods from your Blade templates, you are dealing with a classic architecture question: how do you scope global functionality without creating spaghetti code? The answer lies in understanding where state lives in the Laravel request lifecycle. You generally have three distinct paths depending on whether the function needs to be static, instance-based, or tied to the application context.

Service Classes as the Primary Solution

The most robust approach for shared business logic is using a dedicated Service class. In modern PHP development, we separate concerns by moving complex methods out of controllers and into services. This keeps your controllers thin and focused solely on routing requests and returning responses.

Create a new service file, perhaps named HelperService.php or UtilityService.php, located in app/Services. Define your global function within this class as a static method if it does not depend on database state or the application context, or as an instance method if it requires access to repositories or configuration.

namespace App\Services;

class HelperService
{
    public static function formatCurrency($amount)
    {
        return number_format((float)$amount, 2);
    }

    public function getGlobalData()
    {
        // Logic that requires an instance (e.g., accessing config or DB)
        return config('app.name');
    }
}

In your HomeController and BackendController, you simply call this method. If the method is static, inject it directly: $this->formatCurrency($amount). If it is an instance method, create a single instance of the service at the top of your controller file or inject it via dependency injection in your constructor. This pattern ensures that logic changes only need to be made in one place.

The Helper Facade Pattern

For very simple utility functions that do not require any dependencies and should feel like global helper functions, a Facade is often the cleanest solution. Laravel's Facade pattern allows you to access classes statically, mimicking the behavior of global functions without polluting the namespace or creating static state in models.

Create a new class, such as GlobalHelper, in the app/Http/Helpers directory (or simply app/Facades). Ensure the class is public and contains your desired methods. You can then add an alias in your config/app.php file under the aliases array:

'app.Services.GlobalHelper' => Illuminate\Support\Facades\Facade::class,
// Or more specifically for a custom facade if you extend one

However, the most common and idiomatic way to create "global" functions in Laravel without complex facades is using Service Providers. Create a service provider class, perhaps GlobalHelperServiceProvider, and register it in your app's configuration. In the boot method of this provider, use Laravel's container aliasing or simply rely on static access if you define the methods as static within a generic helper file included via an autoloader pattern.

A simpler alternative often overlooked is creating a standard PHP class that extends Illuminate\Support\Facades\Facade. However, for pure utility functions, many developers prefer placing them in a dedicated namespace like App\Helpers and including the file or using a facade alias.

If you choose to use a Facade approach, ensure your class extends \Illuminate\Support\Facades\Facade and set the static::bindInContainer() method to point to your actual implementation. This allows you to call GlobalHelper::formatCurrency($amount) from anywhere in your application, including Blade files via {{ GlobalHelper::formatCurrency($amount) }}.

Using Service Providers for Autoloading

If you want true global functions that don't require a class prefix, you can utilize a Service Provider to register static methods. Create a service provider and define your methods as static properties or use a closure-based approach if needed. However, the most maintainable way to achieve "global" access without cluttering global scopes is via a custom Facade alias in config/app.php or by extending the facade base class.

For example, you might want a function like Helper::getAdminUrl(). Define this logic in a class, register it as an alias in your service provider, and then call it statically. This keeps the code clean while avoiding the need to instantiate classes everywhere.

Blade Template Access

Accessing these functions from Blade files is straightforward once the class is defined and accessible. If you are using a Facade or a Service class aliased correctly, you can simply use the static call syntax within your view:

@if(GlobalHelper::isAdmin())
    <p>Welcome Admin</p>
@endif

This works seamlessly because Blade supports PHP expressions and static calls. Ensure that your service provider is registered in config/app.php so that the class is bound to the application container before any routes are executed.

Trade-offs and Best Practices

While creating global functions seems like a silver bullet, there are trade-offs to consider. Static methods can make testing difficult because they hold state implicitly or rely on static properties. If your function needs to interact with the database or user session, prefer instance-based services injected into controllers rather than static helpers.

Also, avoid using global keywords in PHP for application logic. They create tight coupling and make refactoring painful. Using a Service Provider to register an alias or creating a Facade provides a cleaner namespace boundary. Finally, always keep your helper classes in logical directories like app/Services or app/Facades to maintain a clear separation of concerns.

If you are looking for more advanced patterns on handling global state or complex service registration in Laravel, resources like https://laravelcompany.com often provide deep dives into architectural best practices that align with these principles.

Our network