Hey guys,
Im trying to implement repositories in my application structure.
I cannot seem to pinpoint the problem here. I get the following error message:
ReflectionException
Class Yourday\Repositories\Listings\DbListingRepository does not exist
My folder structure is as follows:
app
Yourday
Repositories
Listings
My Class:
<?php namespace Yourday\Repositories\Listings;
class DbListingRepository {
public function getAll()
{
return Listing::with('listings')->get();
}
public function find($id)
{
return Listing::findOrFail($id);
}
}
My controller:
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Yourday\Repositories\Listings\DbListingRepository;
class ListingsController extends \BaseController {
public function __construct(DbListingRepository $listing) {
parent::__construct();
$this->beforeFilter('csrf', array('on'=>'post, patch, update', 'delete'));
$this->listing = $listing;
}
My composer.json file:
"psr-0": {
"\\Yourday": "app/Yourday/"
},
What am I doing wrong here?
Hi, you need to add your repository to the IoC container using a Service Provider.
See the App::bind() function for this feature here: http://laravel.com/docs/ioc#basic-usage
Remember add your Service Provider to the app.php providers list.
For Service Provider see: http://laravel.com/docs/ioc#service-providers
Hope it helps you.
Hey CodeATbusiness,
But isn't the service provider more of an optional step to abstract even further? I mean, shouldnt the repository work "on it's own"?
Maybe I misunderstood something here :)
You're right; the service provider isn't required.
Instead of the psr-0
option in composer.json
, try to adjust the autoload > classmap section like this:
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php", // Don't forget the comma here :)
"app/Yourday"
]
},
This won't give you the psr standard of course, but it should let us know if we're missing something else.
Reached said:
Hey CodeATbusiness,
But isn't the service provider more of an optional step to abstract even further? I mean, shouldnt the repository work "on it's own"?
Maybe I misunderstood something here :)
You're correct, you only need to use a service provider if you're binding an interface to an implementation. Do your filenames and folders match the namespace exactly, including capitalization?
Sorry friends, I was thinking that you need to bind to an implementation as thepsion5 commented us.
Thanks for your reply.
Shouldn't psr-0 be like
"psr-0": {
"Yourday": "app/"
},
And psr-4:
"psr-4": {
"Yourday\\": "app/Yourday"
}
And don't forget to "composer dump-autoload"
Seems like you solved my problem, thanks guys!
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community