Organizing Laravel helpers using Composer
As a Laravel project grows, it is common to see the same small functions repeated in multiple places such as formatting values, checking routes, or handling basic text transformations. Instead of scattering these across controllers or traits, a more structured approach is to collect them into a dedicated helper file. While Laravel does not include a default setup for this, Composer's autoload configuration makes it straightforward to register a custom helper file. This ensures those functions are always available throughout the application without manual includes.
Prerequisites
Before setting up custom helper files, a Laravel project must already exist and be working locally. If starting from scratch, the following guide provides step-by-step instructions to create a fresh Laravel project using Composer: Create Laravel 12 project from scratch.
This includes installing Composer, creating the project, and running the built-in development server. Make sure everything is running as expected before moving forward.
Creating the helper file
Instead of placing the helper file inside the app
directory, it can be created directly in the project root for easier visibility and quicker access. This approach works just as well and is often preferred when the file serves a global purpose and is not tied to a specific part of the application structure.
Create the file in the root of the Laravel project:
touch helpers.php
Next, add one or more reusable functions inside:
<?php
if (! function_exists('format_price')) {
function format_price(float $amount, string $currency = '$'): string {
return $currency . number_format($amount, 2);
}
}
if (! function_exists('is_active_route')) {
function is_active_route(string $route): string {
return request()->routeIs($route) ? 'active' : '';
}
}
Each function is wrapped in a function_exists
check to prevent conflicts in case the file gets included more than once. These small checks are a simple way to avoid accidental redeclaration errors when maintaining or scaling the project.
Registering the helper file using Composer
To make your helper functions automatically available throughout the application, you'll need to tell Composer to load the file. Open your composer.json
file and find the autoload
section. It usually looks like this by default:
"autoload": {
"psr-4": {
"App\\\\": "app/"
}
}
You can add a files
key under autoload
like so:
"autoload": {
"psr-4": {
"App\\\\": "app/"
},
"files": [
"helpers.php"
]
}
After modifying the configuration, you need to dump the autoload files:
composer dump-autoload
Now your helper functions will be available globally in the application.
Using the helpers in your code
Once the helpers are registered through Composer, you can use them anywhere in your Laravel app without importing or including anything manually.
Inside a controller:
public function show(Product $product): Response
{
$formattedPrice = format_price($product->price);
return response()->view('product.show', ['price' => $formattedPrice]);
}
In a Blade view:
<span class="{{ is_active_route('products.index') }}">Products</span>
These functions behave just like Laravel's built-in helpers, and they're available everywhere: controllers, views, services, and even middleware.
Conclusion
Helper functions should be used with care. In many cases, logic is better placed in service classes, view components, or dedicated utilities. But if there is a genuine need for global functions, it is important to keep them organized and easy to manage. Grouping them by topic and loading them through Composer helps maintain clarity and avoids cluttering the project with scattered, hard-to-track code. A bit of structure goes a long way when helper files start to grow.
0 Comments