0

Im having trouble with my Laravel application and PSR-4 autoloading. I read that you can have multiple 'sub directory' structure in namespaces like this...

VendorName\ApplicationName\(SubDir)\ClassName

Basically I have my user repositories like so...

Acme\Repositories\EloquentUserRepoistory
Acme\Repositories\UserRepositoryInterface
Acme\Repositories\Repository
Acme\Repositories\RepositoryServiceProvider

All with the namespace Acme\Repositories, but i want to have this directory structure...

Acme\Repositories\UserRepository\EloquentUserRepoistory
Acme\Repositories\UserRepository\UserRepositoryInterface
Acme\Repositories\Repository
Acme\Repositories\RepositoryServiceProvider

but when I move the user repo and interface to the folder UserRepository, i get class not found?

Im sure im just not getting something really simple about namespaces here?

EDIT

I guess what im asking is how to have the additional directory UserRepository to keep the different repositories separate from say OrderRepositories?

Jimmy Howe
  • 185
  • 1
  • 1
  • 13

1 Answers1

0

If you moved UserRepositoryInterface to Acme\Repositories\UserRepository\UserRepositoryInterface.php directory, you need to put UserRepositoryInterface in correct namespace:

<?php

namespace Acme\Repositories\UserRepository;

interface UserRepositoryInterface {

}

and then in other files to access you need to also use this new namespace and not the old one

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
  • OK, here's what I done... i made a directory at Acme\Repositories\UserRepositories and placed UserRepositoryInterface inside it, I changed the namespace inside the file to Acme\Repositories\UserRepositories... but PHPStorm throws a fit, saying 'Undefined Constant UserRepositories'... php debug says "Interface 'Acme\Repositories\UserRepositoryInterface' not found" ?? – Jimmy Howe Oct 24 '14 at 08:05
  • And why you use `Acme\Repositories` when you moved it into `Acme\Repositories\UserRepositories` and in the question you asked about `Acme\Repositories\UserRepository` ? It's not the same – Marcin Nabiałek Oct 24 '14 at 08:07