You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Filament has excellent support for dependency injection in most actions. However, when we attempt to customize the default CreateRecord or EditRecord pages by injecting application-specific classes, it results in an error.
Example of the code:
class CreateModel extends CreateRecord {
publicfunction__construct(privatereadonlyMyComplexDomainCreatorService$modelCreator) {}
}
The application fails to resolve the constructor parameters, producing an error similar to the following:
App\Filament\Resources\ModelResource\Pages\CreateModel::__construct(), 0 passed in /path_to_project/vendor/livewire/livewire/src/Features/SupportPageComponents/SupportPageComponents.php on line 211 and exactly 1 expected
This feature is essential when working with complex domain logic, as it allows us to separate the logic from the Filament page itself while retaining all the default page features.
Current Workarounds
To handle this limitation, developers typically resort to:
Using model or application events.
Using the FilamentPage Lifecycle methods.
Creating custom pages, but being forced to define all the form properties and functions that the default CreateRecord & EditRecord already provides.
While these workarounds are functional, they can introduce unnecessary complexity in scenarios where dependency injection could simplify the process by allowing us to directly inject the required classes into the page constructors.
Proposed Solution
To address this issue, I propose making small changes to the following files to enable support for dependency injection in page constructors:
In a few words, we suggest chaging the lines like:
new$class;
// or new$component;
// by:app($class)
// or app($component::class)
These changes would allow Filament pages to utilize the full benefits of dependency injection, improving flexibility and reducing reliance on complex workarounds.
Benefits
Simplifies handling of complex domain logic in Filament pages.
Reduces the need for model or application events in scenarios where dependency injection is more appropriate.
Improves code organization and maintainability by decoupling domain logic from page files.
Conclusion
This feature would enhance Filament's flexibility and usability for developers working on complex applications, aligning with modern software development practices. Thank you for considering this request!
I would be glad to work in a pull request on this feature if needed.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Problem Description
Filament has excellent support for dependency injection in most actions. However, when we attempt to customize the default
CreateRecord
orEditRecord
pages by injecting application-specific classes, it results in an error.Example of the code:
The application fails to resolve the constructor parameters, producing an error similar to the following:
This feature is essential when working with complex domain logic, as it allows us to separate the logic from the Filament page itself while retaining all the default page features.
Current Workarounds
To handle this limitation, developers typically resort to:
While these workarounds are functional, they can introduce unnecessary complexity in scenarios where dependency injection could simplify the process by allowing us to directly inject the required classes into the page constructors.
Proposed Solution
To address this issue, I propose making small changes to the following files to enable support for dependency injection in page constructors:
SupportPageComponents
(Livewire\Features\SupportPageComponents\SupportPageComponents.php
):gatherMountMethodParamsFromRouteParameters
method.resolvePageComponentRouteBindings
method.ComponentRegistry
(Livewire\Mechanisms\ComponentRegistry.php
):new
method.In a few words, we suggest chaging the lines like:
These changes would allow Filament pages to utilize the full benefits of dependency injection, improving flexibility and reducing reliance on complex workarounds.
Benefits
Conclusion
This feature would enhance Filament's flexibility and usability for developers working on complex applications, aligning with modern software development practices. Thank you for considering this request!
I would be glad to work in a pull request on this feature if needed.
Beta Was this translation helpful? Give feedback.
All reactions