if you pass the class on your constructor you can test your functions if you don't, will be really hard test your code
For an extensive read, click here.
In short, the way DI works in L4 is you pass the class you want in the constructor, as you would in regular PHP. Then, somewhere in your application (I usually create a file in my app directory for this), bind the name you used in the constructor type hint to an actual class. I know – sounds a little vague. Here's an example:
class MyController extends BaseController
{
protected $userRepo;
public function __construct(UserRepositoryInterface $userRepo)
{
$this->userRepo = $userRepo;
}
}
Then all you have to do is bind UserRepositoryInterface to a class like so:
App:bind('UserRepositoryInterface', 'DbUserRepository');
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community