ApplicationBuilder
and DX
#55390
Unanswered
andrii-androshchuk
asked this question in
Ideas
Replies: 1 comment
-
Anything would be better than the current laravel 11/12 sausage (chained) way of blindly (if you don't have documentation with examples you are doomed) bootstrapping the application. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Motivation
When starting a new Laravel application—especially using the
ApplicationBuilder
pattern inbootstrap/app.php
— I encountered several limitations and DX (developer experience) concerns. The current approach, where a single “plain” PHP file is responsible for creating and configuring the entire application, feels inefficient and less maintainable in the long run. In this discussion, I want to highlight the issues I faced and propose a few suggestions for your review and feedback.Problems & Solutions
Application Bootstrapping as a plain PHP File
The application is currently created and returned from a single, plain PHP file via
require_once
. While this is simple, I believe it's not ideal for more complex applications. Additionally, this logic lives outside theApp/
namespace.Suggestion: Introduce a dedicated class — perhaps something like [
App
|Kernel
|AppFactory
| ... ], that will be located inApp/
namespace — to encapsulate the creation and configuration of the application. This class could expose acreate()
orbuild()
method and be referenced directly from the front controller (e.g.,public/index.php
).Provider Loading as a Separate File
There is another plain PHP file (
bootstrap/providers.php
) just to return an array of providers. This again feels unnecessarily constrained and with limited flexibility.Suggestion: Move this logic into a method of the same proposed [
App
|Kernel
] or builder class. That way, the list of providers can be built dynamically based on the environment or other conditions. This opens up more flexible and expressive configuration options, including use of private methods, composition, or other classes for modular setup.Reducing Root-Level Directories
If the logic from
bootstrap/app.php
andbootstrap/providers.php
is moved into a dedicated class, the only remaining directory inbootstrap/
isbootstrap/cache
.Suggestion: Consider moving
bootstrap/cache
tostorage/framework/bootstrap
to reduce clutter in the root and keep all writable directories understorage/
. This could be done manually with$app->useBootstrapPath(...)
but why not making it a standard practice and keep all writable directories only understorage/
.Inconsistencies in with*() Methods
Another set of concerns relates to the inconsistency in how
with*()
methods are defined and used in theApplicationBuilder
, here are the examples:A few issues I’ve encountered:
withRouting()
, it accepts both a$using
and a$then
callback:$using
is called via$this->app->call(...)
, so it receives full dependency injection.$then
is just directly invoked with$then($this->app)
, with the container instance, but without DI.Suggestion: Standardize how callbacks are handled. Ideally, all should accept
Closure|callable
, and all should be invoked through the container (e.g.,$this->app->call(...)
) to allow for proper DI.Summary
To summarize, here are the proposed improvements:
bootstrap/app.php
andbootstrap/providers.php
with a dedicated builder class [App
|Kernel
].bootstrap/cache
intostorage/framework/
for cleaner project structure, and removebootstrap
from the root.with*()
method signatures and invocation strategy for better consistency DI, and DX.Here is the example of the final class:
The resulting project structure after applying these suggestions would look as follows:
I’d love to hear your thoughts and feedback on the proposed solution.
Thank you.
Beta Was this translation helpful? Give feedback.
All reactions