Last active
September 11, 2015 13:52
-
-
Save andersao/7b92fa026fd4ffe74fbb to your computer and use it in GitHub Desktop.
Exemplo de implementação do Repositório
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//------------------------------------ | |
// StudentRepository.php | |
//------------------------------------ | |
namespace App\Repositories\Students; | |
use Prettus\Repository\Contracts\RepositoryInterface; | |
/** | |
* Interface StudentRepository | |
* @package App\Repositories\Students | |
*/ | |
interface StudentRepository extends RepositoryInterface {} | |
//------------------------------------ | |
// StudentEloquentRepository.php | |
//------------------------------------ | |
namespace App\Repositories\Students; | |
use Prettus\Repository\Eloquent\BaseRepository; | |
/** | |
* Class StudentEloquentRepository | |
* @package App\Repositories\Students | |
*/ | |
class StudentEloquentRepository extends BaseRepository implements StudentRepository { | |
function model() | |
{ | |
return "App\\Student"; | |
} | |
} | |
//------------------------------------ | |
// No seu Service Provider | |
//------------------------------------ | |
$this->app->bind(StudentRepository::class, StudentEloquentRepository::class); | |
//ou | |
$this->app->bind('App\Repositories\Students\StudentRepository', 'App\Repositories\Students\StudentEloquentRepository'); | |
//------------------------------------ | |
// Usando o Repositório no seu Controller | |
//------------------------------------ | |
use App\Repositories\Students\StudentRepository; | |
/** | |
* Class StudentController | |
* @package App\Http\Controllers | |
*/ | |
class StudentController extends Controller { | |
protected $repository; | |
/** | |
* @param Student $model | |
*/ | |
public function __construct(StudentRepository $repository){ | |
$this->repository = $repository; | |
} | |
public function index(){ | |
return Response::json($this->repository->all()); | |
} | |
public function show($id){ | |
return Response::json($this->repository->find($id)); | |
} | |
//... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment