Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

AliShahbaz's avatar

How to add External Files into Laravel Project ?

hi i have a laravel project and i want to add a file or a function into my laravel project how can i add it. Please tell me

0 likes
2 replies
bobbybouwmann's avatar

Well it depends on the file or function, however you can really easily add a helpers file where you can put in methods that you can reuse throughout the framework.

Create a helpers.php file in your app directory.

And then in your composer.json file add the following "files" array to the autoload section. It should look something like this:

"autoload": {
    "classmap": [
        "database/seeds",
        "database/factories"
    ],
    "files" : [
        "app/helpers.php"
    ],
    "psr-4": {
        "App\": "app/"
    }
},

Run composer dump-autoload -o and you can use the methods in that file throughout your project ;)

pardeepkumar's avatar

Create a helpers.php file in your app folder and load it up with composer:

"autoload": {
    "classmap": [
        ...
    ],
    "psr-4": {
        "App\": "app/"
    },
    "files": [
        "app/helpers.php" // <---- ADD THIS
    ]
},

After adding that to your composer.json file, run the following command:

composer dump-autoload

Now on every request the helpers.php file will be loaded automatically because Laravel requires Composer’s autoloader in public/index.php:

require DIR.'/../vendor/autoload.php';

Defining Functions

Defining functions in your helpers class is the easy part, although, there are a few caveats. All of the Laravel helper files are wrapped in a check to avoid function definition collisions:

if (! function_exists('env')) {
    function env($key, $default = null) {
        // ...
    }
}

Please or to participate in this conversation.