When looking at the source of Laravel, I noticed a lot of these things:
Controller Class:
class Controller extends BaseController { use AuthorizesRequests, AuthorizesResources, DispatchesJobs, ValidatesRequests; }
One of its component features:
trait AuthorizesRequests { public function authorize($ability, $arguments = []) { list($ability, $arguments) = $this->parseAbilityAndArguments($ability, $arguments); return app(Gate::class)->authorize($ability, $arguments); }
I have a few questions about this:
- Does this template (abstracting reuse functionality in features) have a name?
- Is this template useful for any other projects?
- If a trait requires dependencies, is there a better way to implement them, instead of using a service locator (e.g.
app() ) in this case?
I am considering using this approach in my code to share some common functions between several of my classes. I am considering the possibility of creating a ChecksBarcodes attribute that will work with the inventory information store and share this attribute between several similar but unrelated product management classes that everyone should check for barcodes.
source share