[5.4] Why do service providers depend on helper functions? #18506
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Dear Taylor and amazing Laravel community;
I'm working on some projects in which it is not possible to rely on core helper functions. The reason for this isn't trivial to explain, but I'll try to be concise. The codebase incorporates more than one Laravel-based application such that it is not possible to use
Container::getInstance()
, since more than one instance of the Container may exist in a single request.Because of this constraint, it is also not possible to use the global helper functions, all of which rely on
Container::getInstance()
for accessing what would normally be the only container in the request.The simple change I am requesting is that service providers, already having in
ServiceProvider::$app
the instance ofContainer
that represents their relationship to the application in which they are configured and installed, should use that instance for accessing theContainer
's contents instead of invoking helper functions.The specific case I have highlighted below focuses on
Illuminate\Mail\MailServiceProvider
, but I believe there are other examples, includingIlluminate\Database\DatabaseServiceProvider
,Illuminate\Pagination\PaginationServiceProvider
, andIlluminate\Scout\ScoutServiceProvider
.In addition to ending the practice of using helper functions within service providers, I would like to recommend that the usefulness of being able to pass a subpath argument to
resource_path($path='')
be refactored into the aliased functionIlluminate\Foundation\Application::resourcePath()
such that this convenience can continue even without the helper function in play.Thank you for your consideration.
Sincerely,
Aaron