I use dependency injection repository.
How would it be:
Create a folder inside the folder in your project app
with the name repository
. Inside the folder repository
create a php file that will be a default interface with the name of RepositoryInterface
with this content.
<?php
interface RepositoryInterface {
public function all();
public function toDropDown();
public function toJson();
public function toArray();
public function toListPaginate();
public function remove($id = NULL);
public function get($id = NULL);
public function create();
}
Generally the interface
is created by some purpose being such a model itself. This being a basis I create the interface on this so that at the time of the injection I have standard names. Example RepositoryCreditoInterface
, that is, create another PHP file with this content following the example
<?php
interface RepositoryCreditoInterface extends RepositoryInterface { }
Implement the RepositoryCreditoInterface
creating a new PHP file, named RepositoryCredito
, having the contents implemented and with coding.
<?php
class RepositoryCredito implements RepositoryCreditoInterface {
protected $nameTable = 'creditos';
public function __construct() { }
public function create(){
return new Credito();
}
public function get($id = NULL){
if ($id){
return Credito::find((int)$id);
}
return NULL;
}
public function remove($id = NULL)
{
if ($id) {
$model = $this->get($id);
if ($model) {
$model->delete();
return true;
}
}
return false;
}
public function toListPaginate()
{
return
DB::table($this->nameTable)
->where('descricao','LIKE', '%'.Input::get('filtro', '').'%')
->orderBy('descricao','asc')
->paginate(10);
}
public function all(){
return Credito::orderBy('descricao')->get();
}
public function toJson(){
return Credito::orderBy('descricao')->get()->toJson();
}
public function toArray(){
return Credito::orderBy('descricao')->get()->toArray();
}
public function toDropDown(){
return Credito::orderBy('descricao')->lists('descricao', 'creditoid');
}
}
Understand how about the RepositoryCredito
is from Model
Credito
(referring to the table creditos
).
All these files RepositoryInterface
, RepositoryCreditoInterface
and RepositoryCredito
everything inside that briefcase app\repository
.
So that these classes go up to Laravel goes in the folder app\start\global.php
she’ll be like this:
ClassLoader::addDirectories(array(
app_path().'/commands',
app_path().'/controllers',
app_path().'/models'
app_path().'/database/seeds',
));
Add it to look like this:
ClassLoader::addDirectories(array(
app_path().'/commands',
app_path().'/controllers',
app_path().'/models',
app_path().'/repository',
app_path().'/database/seeds'
));
That is, the files were added from that folder repository
(app_path().'/repository'
) to your project Laravel.
Now in the folder app\
create a file named after ioc.php
having in its contents:
<?php
/*
|--------------------------------------------------------------------------
| App::bind IOC
|--------------------------------------------------------------------------
|
*/
App::bind('RepositoryCreditoInterface', 'RepositoryCredito');
Note: all interfaces you can register in this file giving a better organization to your project.
For this file to work will also go again on app\start\global.php
in the last line add like this:
require app_path().'/ioc.php';
After these settings, so that such dependency injection into the controllers (Controller
) it’s just pass as in the builder (__construct
) the interface you want to be solved.
Example:
<?php
class CreditoController extends BaseController {
/*
* construct
*/
public function __construct(RepositoryCreditoInterface $repository) {
parent::__construct();
View::share('titleView', 'Crédito');
$this->repository = $repository;
}
Using in the index:
public function index() {
$model = $this->repository->toListPaginate();
View::share('routeUpdate', 'admin.credito.update');
return View::make('admin.credito.index')
->with('model', $model);
}
Note: In the constructor you can pass several interfaces
Right, so on the interface I’ll put the 'main methods', and if I wanted another method any put right into the repository? Sorry it took me so long to answer.
– Patrick Maciel
@Patrickmaciel, then, will depend on the method in question, the place where you will put, if it is equal to all put in Repositoryinterface and implement in all Repository, if it is something specific put in Repositorycreditointerface as mentioned in the example. I always like to put on everyone so that everyone has the same contract, facilitates maintenance and your project is all standardized.
– user6026
Right. What about Model? This way, I’ll never have to create any method in it? Its function will be only as to table name, relationships, etc?
– Patrick Maciel
In the model you leave normal and put the methods in the interface implementing in Repository
– user6026