Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

RhythmScout's avatar

Using Multiple Repositories in Controller

I'm trying to determine the best way to include a reference to more than one repo in a single controller.

I've got a "SubjectsController" which very closely follows the standard repository pattern:

class SubjectsController extends BaseController
{

    /**
     * @var Cmapp\Interfaces\SubjectRepositoryInterface $repo
     */
    public $repo;

    /**
     * __construct method
     * Inject our Subject repository
     *
     * @param SubjectRepositoryInterface $repo
     */
    public function __construct( SubjectRepositoryInterface $repo )
    {
        $this->repo = $repo;
    }

I'm fine with all of this. My issue is in my "create" method, I need to pass a list() from another model to my view for a simple form drop down.

public function create()
    {
        # List of available studies for assignment
        $studies = Study::lists('name', 'id');

        return View::make('manager.subjects.create')->with(compact('studies'));
    }

How do I access my Study repo (I do have one), so that I'm not dependent on the Eloquent Model in my controller? Do I inject it in my controller constructor alongside my Subjects repo? Do I make it available to my view using a View Composer? What's the "best practice" here?

Seems like overkill to DI this for a single method in my controller. I have tried this approach and it works fine, just not sure if this is the correct thing to do.

0 likes
3 replies
bestmomo's avatar
Level 52

I dont know if it's the "best practice" but I have many cases where i must inject 2 repositories in a controller. It's simple and easy for tests.

anchour's avatar

If it works, it works. It doesn't seem like there's any issue with having two repository interfaces injected into a controller, so long as they're concise and achieve what you need them to do. It's not like you're injecting 8 classes into the constructor. Plus, easier testability and all that fun stuff.

RhythmScout's avatar

Thx to both for the confirmation. That was my initial thought, but as I looked around to try to find examples of this, I couldn't find anything anywhere. I thought it would be a common practice but all examples simply had a single DI injection of the directly related model (interface) in the controller. It does work fine this way, so staying with it.

Please or to participate in this conversation.