Roots Discourse

How to register a simple Laravel ServiceProvider?

Hi,
I’d like to require an external service provider (in my case mtownsend/read-time).
after doing composer require mtownsend/read-time, I added the service provider to config/app :

'providers' => [
        App\Providers\ThemeServiceProvider::class,
        Mtownsend\ReadTime\Providers\ReadTimeServiceProvider::class,
    ],

I get the following error:

Call to undefined function Mtownsend\ReadTime\Providers\config_path()

The package uses Illuminate\Support\ServiceProvider\config_path function, while I’d like it to use Roots\Acorn\ServiceProvider's one.

If I change the used namespace and call \Roots\config_path('read-time.php') instead of config_path('read-time.php') in the packages files it works, but I guess it’s not the right way to do.

What would be the best practice in this case ? Is there a way to “override” a namespace a package is using ?

No one’s got an answer for this ?
Should I override the service provider’s class ?
Would be annoying to do this each time I require a service provider…

I’ve got something similar, but not quite the same. Within the services I’ve created (and that are registered by the Service Providers), I’m getting namespace issues when, within a method of the Service, I try and call a plain old function that exists elsewhere (“Call to undefined function…”) - it’s trying to find it under the namespace of the Service, but then even if I prefix the function with “” to use the root namespace, it’s still not finding it (yet elsewhere in the site when I call the plain function, it’s working fine).

I’m literally troubleshooting now - will post back if I find anything as it might be related in some way.

I guess you meant \ , right?

Yeah - ha, it got stripped out.

I’m now using a (namespaced) class with static methods that contain the logic of the plain functions - all works fine. Even if I namespaced the file containing the plain functions they didn’t work, so I’m not sure what it is about static methods on a class that “fixes” it - perhaps a lack of understanding on my part.

Did you try setting this to true? https://github.com/roots/sage/blob/master/config/app.php#L83

There’s no namespace in the top of the file where the functions are defined? If not it sounds like the file with the function declarations hasn’t been included by the time your methods try to invoke the functions.

I had tried with and without namespacing the file containing functions.

All the functions were accessible everywhere apart from inside services registered into the container.

At first the functions were attempted to run under the namespace of the service, eg my_function() was being called as App\Providers\SomeService\my_function()

When I called the function like \my_function() or after namespacing the functions file App\SomeNamespace\my_function() it couldn’t be found.

Using App\Utils\SomeUtilClass::my_method() works.

It might be a timing thing but the function files were included early before Acorn boots up.

Must be a (auto)loader related thing rather than it being a namespace issue.