Laravel helper files not working?
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Laravel Helper Files Not Working? Decoding the "Call to undefined function" Error
If you are working within a Laravel application and find yourself trying to call functions defined in helper files—like app/helpers.php—only to encounter an error such as Call to undefined function App\Http\Controllers\test(), it can be incredibly frustrating. You might have copied setup instructions from another project, yet the fundamental mechanism seems broken.
This issue usually stems not from a simple missing autoload step, but from a deeper misunderstanding of how Laravel structures its application code and manages namespaces versus plain PHP file inclusion. As a senior developer, let's dive into why this happens and how to fix it correctly.
The Misconception: Autoloading vs. Function Scope
The core problem lies in the difference between how Composer handles class autoloading (which is Laravel’s primary mechanism) and how simple PHP files are loaded as functions. When you place a file like app/helpers.php, simply placing it in the directory doesn't automatically make its functions globally available to your controllers unless you explicitly tell PHP or Laravel how to find them.
When you call a function directly from a controller, Laravel expects that function to be either an accessible method on an object, a static method, or defined within a properly namespaced class that has been loaded via the Composer autoloader. A plain file included this way often fails because PHP cannot resolve the function's location based on standard PSR-4 conventions.
Solution 1: The Laravel Best Practice – Encapsulation via Classes
The most robust and maintainable way to handle reusable logic in a large application is to avoid global helper files entirely. Instead, encapsulate your helper logic within dedicated classes and register them using Laravel's Service Container or Service Providers. This aligns perfectly with the object-oriented principles that power frameworks like Laravel.
Instead of defining functions globally, define methods within a class:
// app/Helpers/MyHelper.php
namespace App\Helpers;
class MyHelper
{
public function test()
{
dd(":(");
}
}
To use this in your controller, you would typically inject the class or instantiate it:
// app/Http/Controllers/TestController.php
namespace App\Http\Controllers;
use App\Helpers\MyHelper; // Import the helper class
class TestController extends Controller
{
public function test(MyHelper $helper) // Dependency Injection
{
$helper->test();
}
}
This approach ensures that your code follows the principles promoted by the Laravel ecosystem, making testing and dependency management significantly easier. For deeper insights into framework architecture, always refer to resources like laravelcompany.com.
Solution 2: Direct File Inclusion (If Necessary)
If you absolutely must include raw helper functions, you need to ensure they are loaded explicitly by the PHP runtime before the controller attempts to call them. Simply placing a file in app/ is insufficient for this direct access.
A more controlled method involves using an entry point or a custom autoloader setup, though this is generally discouraged for application logic. If you insist on loading it directly, you must use require_once at the very top of your main entry files (like public/index.php), ensuring that the file is loaded in a context accessible to all parts of the application.
Avoid placing custom functions outside of class structures. Relying on standard Laravel patterns will prevent these kinds of mysterious errors and keep your codebase clean, reinforcing best practices outlined by teams working with systems like those at laravelcompany.com.
Conclusion
The error you are seeing is a classic symptom of mixing procedural PHP file inclusion with Laravel’s object-oriented, class-based architecture. Stop treating helper files as global function repositories and start treating them as encapsulated classes within the Service Container. By adopting dependency injection and proper class structure, you ensure your application remains scalable, testable, and free from these frustrating runtime errors.