Blade Components - call attribute setters #43082
-
I would love to have the ability to call a setter method when using a Blade Component, rather than forcing the parameters to be in the constructor of the class. For example:
currently (as I understand it) needs:
However, what would be awesome would be to be able to do:
where On the face of it, this does the same thing, but I have a suite of components with various shared features and I'd love to work better with Traits and inheritance. The insistence on constructor params is unwieldy and leads to a lot of code duplication. For example of a possible use case, a subset of my components might all use a facility whereby they're read-only to certain people (based on roles or some other logic). I would like to be able to easily add that to any component, so some form of If I then set a I've been digging into the Service Container binding stuff, and I can't see how to make this work. It's almost like, in the build() method after it's resolved the constructor params, it could look for setter methods for any parameters it didn't use... maybe that's too simplistic... Or, is there a way to manually register the setters as something the Container would call while resolving the component? Or another approach that I'm missing? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Never mind - after a load of digging, I've found the 'withAttributes' call which passes any variables not in the constructor into the view as $attributes. That might solve most of what I was needing, just without going via setters etc. |
Beta Was this translation helpful? Give feedback.
Never mind - after a load of digging, I've found the 'withAttributes' call which passes any variables not in the constructor into the view as $attributes. That might solve most of what I was needing, just without going via setters etc.