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

JJK's avatar
Level 1

Setting up ServiceProvider and Facade with alias from a custom package

Hello,

I'll try to be as quick as possible.

I'm making a custom package, the file structure is as follows:

/packages/jtinkers/borgcms/src/composer.json
/packages/jtinkers/borgcms/src/BorgCMS/FluidSchema/FluidSchema.php
/packages/jtinkers/borgcms/src/BorgCMS/FluidSchema/FluidSchemaFacade.php
/packages/jtinkers/borgcms/src/BorgCMS/FluidSchema/FluidSchemaServiceProvider.php

Okay, now here's the important part of composer.json:

    "autoload":
    {
        "psr-4":
        {
            "BorgCMS\": "src/BorgCMS/"
        }
    },
    "extra":
    {
        "laravel":
        {
            "providers":
            [
                "BorgCMS\FluidSchema\FluidSchemaServiceProvider"
            ],
            "aliases":
            {
                "FluidSchema": "BorgCMS\FluidSchema\FluidSchemaFacade"
            }
        }
    }

FluidSchema.php

namespace BorgCMS\FluidSchema;

class FluidSchema
{
    public function test()
    {
        echo 'test';
    }
}

FluidSchemaFacade.php

namespace BorgCMS\FluidSchema;

class FluidSchemaFacade extends Facade
{
    protected static function getFacadeAccessor()
    {
        return 'fluidschema';
    }
}

FluidSchemaServiceProvider.php

namespace BorgCMS\FluidSchema;

class FluidSchemaServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->bind('fluidschema', function()
        {
            return new FluidSchema();
        });
    }
}

The unimportant bits like comments or boot() method were omitted deliberately, but they are in my project - just empty.

As you can see, it's a standard setup of some class, a facade and a service binding it - but neither fluidschema()->test() nor FluidSchema::test() work and both pop either error about fluidschema being an unknown function or Undefined constant 'BorgCMS\FluidSchema\FluidSchema'

Is there a way to show registered service providers, aliases and facades? Other files like app/config.php remain untouched.

0 likes
12 replies
Nakov's avatar

@jjk does this line breaks your composer.json file maybe, because with a single slash you are escaping the next character..

"BorgCMS\": "src/BorgCMS/"

should be:

"BorgCMS\\": "src/BorgCMS/"

Also I assume this is the composer.json file from your package, right?

1 like
JJK's avatar
Level 1

@nakov

Oh, it's a forum thing, they actually all have double slashes - it's just that using markdowns code container does that.

Here's an image:

https://i.imgur.com/GKipltp.png

Nakov's avatar

@jjk I see now that even in my reply the second one is missing :D

Did you run composer dump-autoload after requiring your package?

JJK's avatar
Level 1

But my package is phisically on my drive in the laravel's project, not loading from github.

The github link I've sent you consists of the entire project I'm using to test the package (see folder: packages)

Nakov's avatar

@jjk I did see that, but you must load it. Check the SO answer that I shared :)

otherwise the package even though it is in the same project, it will not be available. The package composer.json is not being loaded unless you require it in the main/root composer.json file.

Nakov's avatar
Nakov
Best Answer
Level 73

@jjk I downloaded your project locally.. and this is what I did:

in the project composer.json :

"repositories": [
        {
            "type": "path",
            "url": "FULL_PATH/BorgCMS/packages/jtinkers/borgcms"
        }
    ]

Then run: composer require "jtinkers/borgcms @dev"

then composer dump-autoload

In web.php used:

dd(FluidSchema::test());

Works perfectly well :)

JJK's avatar
Level 1

@nakov

Okay, so FluidSchema works - that's good, thank you :)

.. but what about fluidschema()?

Nakov's avatar

@jjk but you have no helper function that is being loaded and I don't see it created at all..

For example the helper function app() is created in a helpers.php file in the framework, and here is the definition:

if (! function_exists('app')) {
    /**
     * Get the available container instance.
     *
     * @param  string|null  $abstract
     * @param  array  $parameters
     * @return mixed|\Illuminate\Contracts\Foundation\Application
     */
    function app($abstract = null, array $parameters = [])
    {
        if (is_null($abstract)) {
            return Container::getInstance();
        }

        return Container::getInstance()->make($abstract, $parameters);
    }
}
JJK's avatar
Level 1

@nakov Ah, okay - then I'll skip creating such a function now that I got what I wanted.

I just noticed that most facades like Auth, View etc. have a shorthand for them auth(), view() (or a lowhand?? lol.) - thought it's something that binding 'fluidschema' in service provider does.

Nakov's avatar

@jjk nope :) take a look at vendor/laravel/framework/src/Illuminate/Foundation/helpers.php: most are defined there :)

Glad I helped! Happy coding :)

1 like

Please or to participate in this conversation.