I've created a custom class OrderRepository with the following:
Custom Class
# path: app/Acme/Repositories/OrderRepository.php
<?php namespace App\Acme\Repositories;
class OrderRespository{
public function __contstruct()
{
# code...
}
}
Controller
# Snippets of what I've added
use App\Acme\Repositories\OrderRespository;
private $order;
public function __contstruct(OrderRespository $order){
$this->order = $order;
}
public function index()
{
dd($this->order);
}
I get a null object when hitting the uri.
When I tried dd(new OrderRepository());, I get the following error:
Class 'App\Acme\Repositories\OrderRespository' not found
Where are your creating your OrderRepository instance? Normally you don't have to dump autoload when creating a new class in the App hierarchy. Need to see your more code. Post your autoload section in your composer.json file.
In order to inject a dependency like that, you need to make App aware of it by registering the class in a service provider. Normally when using repositories, you would bind an interface to the concrete class and then inject the interface rather than the concrete class. Otherwise you're simply exchanging a hard dependency on a model with a hard dependency on another class.
About the service container : https://laravel.com/docs/5.2/container