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

serien's avatar

Dependency injection in API controllers

Hey!

I am trying to create an api using laravel that will serve into a react application. I have the api authentication working correctly (finally!) but I am running into an issue.

The way my app is set up, I have services that I am injecting into my controllers. These are provided and work fine on the web routes, but on my api routes I get "cannot call _ on undefined". It seems they aren't being provided.

Is this expected behavior on laravel, and is there something I can do to ensure my dependencies are there in my api controllers?

0 likes
3 replies
tykus's avatar

That appears to be a javascript error rather than anything PHP/Laravel related

serien's avatar

It is definitely laravel related! The problem isn't with the react, it's with the api laravel controllers.

I am essentially not able to reference any dependencies injected to them. They are all provided as null

serien's avatar
serien
OP
Best Answer
Level 1

Ok! good news - I think this was a caching issue. It seems to have fixed itself by some miracle? (Which is wonderful! I couldn't imagine why the service provider logic would play any differently in api routes than it does in web routes :D )

Please or to participate in this conversation.