I’ve faced the same problem in the last few months, the book Laravel: From Apprentice to Craftsman helped a lot.
Following the model described by him I made some slightly different.
I put my application in a folder inside
app\
app Meuapp
I put my repositories in
app Meuapp Repository
app Meuapp Repository Clientesrepository.php
And I work with a preacher
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 do not usually use interfaces (my fault) on several occasions I needed to instantiate more than 3 of them, I also have the need of my IDE auto detect the methods that each repository has available
/**
* @var \Meuapp\Repository\ClientesRepository
*/
public $clientes;
I could see how confused he was.
So I took advantage of a magical php method to solve this problem the __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 on 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 proved very good to me.
One advantage I see is being able to carry 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’ve helped
You could add the code from 1 of the 2 repositories please?
ClienteInterface
andCliente
for example (just a chunk of code, showing what to insert into the interface, and what to insert into the implementing class)– Patrick Maciel
@Patrickmaciel, I added earlier, I hope you clarify.
– Antonio Carlos Ribeiro