Support the ongoing development of Laravel.io →
posted 10 years ago
Architecture

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?

Last updated 3 years ago.
0

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.

Last updated 3 years ago.
0

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 :)

Last updated 3 years ago.
0

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.

Last updated 3 years ago.
0

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?

Last updated 3 years ago.
0

Sorry friends, I was thinking that you need to bind to an implementation as thepsion5 commented us.

Thanks for your reply.

Last updated 3 years ago.
0

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"

Last updated 3 years ago.
0

Seems like you solved my problem, thanks guys!

Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Reached reached Joined 27 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.