Last active
January 3, 2016 05:09
-
-
Save coreymcmahon/8414001 to your computer and use it in GitHub Desktop.
Building Testable Applications using the Repository Pattern - http://www.slashnode.com/the-repository-pattern/
This file contains hidden or 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 namespace Acme\Controllers; | |
use Acme\Storage\PostRepositoryInterface as PostRepository; | |
use Acme\Models\Post; | |
class PostController extends BaseController { | |
private $postRepository; | |
// notice that our controller now expects PostRepositoryInterface to be injected | |
// during instantiation | |
public function __construct(PostRepository $postRepository) | |
{ | |
$this->postRepository = $postRepository; | |
} | |
public function index() | |
{ | |
// instead of using the model class directly, we use the PostRepositoryInterface | |
// implementor | |
$posts = $this->postRepository->paginate(20); | |
return View::make('post.index', compact('posts')); | |
} | |
public function show($id) | |
{ | |
$post = $this->postRepository->findOrFail($id); | |
return View::make('post.show', compact('post')); | |
} | |
// ... etc | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment