Skip to content

Would you consider supporting DI for the presenters? #52

Open
@judgej

Description

With many other presenter packages out there, I keep coming back to this one for its simplicity and flexibility (others seem to limit themselves to much more restricted use-cases).

Anyway, tried adding a service to a presenter constructor, since the service contains functionality used across a number of the presenter methods:

public function __construct($entity, protected OrderService $orderService)
{
    parent::__construct($entity);
}

This did not work, as the additional parameter was not being set in PresentableTrait:

$this->presenterInstance = new $this->presenter($this);

So this tried this in PresentableTrait instead, and it worked a treat:

$this->presenterInstance = app()->make($this->presenter, ['entity' => $this]); 

Now my order presenter class has $this->orderService at its disposal.

Now, I know I can extend the trait with this fix (or something similar - this is first draft to check viability) for myself, but is this something that could be useful for this package? It should be possible for it to work across all the supported Laravel versions.

Having to include $entity to pass up to the parent is, unfortunately, a little messy.

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions