I faced the same problem in recent months, the book > Laravel: From Apprentice to Craftsman helped a lot.
Following the model he described, I did something a little different.
I put my application in a folder inside
app \
app \ Meuapp
I put my repositories in
app \ Myapp \ Repository
app \ Myapp \ Repository \ ClientsRepository.php
And I work with a provider
app / Meuapp / AppProvider.php
<?php namespace Meuapp\Providers;
use Illuminate\Support\ServiceProvider;
class AppProvider extends ServiceProvider {
public function register(){
$this->app->singleton('RepoClientes', '\Meuapp\Repository\ClientesRepository');
}
}
?>
So I can easily instantiate my repository.
public function __construct(){
$this->clientes = App::make('RepoClientes');
}
In my case I have many repositories and I do not usually use interfaces (my fault) on several occasions I needed to instantiate more than 3 of them, I also have the need for my IDE to auto detect the methods that each repository has available
/**
* @var \Meuapp\Repository\ClientesRepository
*/
public $clientes;
I can see how confused it was.
So I took a magic php method to solve this problem __ get ()
As all my controllers extend BaseController I added the following method to it:
public function __get($var)
{
switch ($var):
case 'clientes':
$this->clientes = App::make('RepoClientes');
return $this->clientes;
break;
endswitch;
}
And about the auto complete of my IDE I add the following line:
/**
* Class BaseController
*
* @property \Meuapp\Repository\ClientesRepository $clientes
*/
class BaseController extends Controller{
public function __get($var){}
}
This may not be the best option, but it has been very good for me.
One advantage I see is being able to load exclusively what my method will need with ease and flexibility.
Remembering that for this to work you need to edit 2 files:
composer.json
"autoload": {
"psr-0": {
"Meuapp": "app/",
}
},
app / config / app.php
'providers' => array(
...
'Meuapp\Providers\AppProvider',
)
I hope I have helped