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

Roni's avatar
Level 33

Adding Helper files from a package to a Laravel App

When writing a package, I want to add two helper files to my laravel application.

Normally I'd just pull them in through composer to the proper namespace. Like this in a laravel App

Composer.json

The Following would pull functions in the app/Helpers/BaseHelpers.php into a global scope in the app namespace, and the functions in tests/test-helpers.php into the test namespace.

autoload": {
    "psr-4": {
      "App\": "app/"
    },
    "files": [
      "app/Helpers/BaseHelpers.php"
    ]
  },

"autoload-dev": {
    "psr-4": {
      "Tests\": "tests/"
    },
    "files": [
      "tests/test-helpers.php"
    ]
  },

However, if I'm writing a package and I want a file to be pushed into global scope of the test namespace in the project consuming it, how would you adjust the composer.json in the package?

0 likes
6 replies
Roni's avatar
Level 33

Sadly no dice, I keep coming back to this and burning 20 min chuncks :( But I just can't figure out any combination of making this happen. I just want to include my global functions but from the package pushing into the app, not from the app side pulling in the package.

In the interest of time, I wimped out and just made a preset command to load my files in the right places and update composer.json. So lame, but at least it won't be a barrier to entry for others.

Talinon's avatar
Talinon
Best Answer
Level 51

@roni

I see what you're saying. I'm not sure if there is a way of doing that thru composer packages.

You could probably place something like this within your package's service provider:

include_once(__DIR__.'/Helpers.php');

Then define all your global helper functions within that file, wrapping each one in a if function_exists()

I don't see why that wouldn't work.

1 like
Roni's avatar
Level 33

Damn ! it works!

I was including it in the service provider but you need to do it before you declare your class!


<?php

namespace LaravelTestingConcerns;

include_once ('funcs.php');

use Illuminate\Support\ServiceProvider as BaseServiceProvider;


class ServiceProvider extends BaseServiceProvider {

    public function boot()
    {
    
    }

    /**
     * Register any package services.
     *
     * @return void
     */
    public function register()
    {
    
    }
    
}

@talinon for the win!!!

1 like

Please or to participate in this conversation.