Forked from taylorotwell/gist:68f614deb9538f2e30108c2698266fda
Last active
January 29, 2018 09:08
-
-
Save MartelliEnrico/0ae021a7b2934a1eef39784273671ebc to your computer and use it in GitHub Desktop.
ADR out of the box for Brandon
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 | |
/* | |
|-------------------------------------------------------------------------- | |
| Application Routes | |
|-------------------------------------------------------------------------- | |
| | |
| Here is where you can register all of the routes for an application. | |
| It's a breeze. Simply tell Laravel the URIs it should respond to | |
| and give it the controller to call when that URI is requested. | |
| | |
*/ | |
use Illuminate\Http\Request; | |
use Illuminate\Routing\Controller; | |
use Illuminate\Support\Collection; | |
use Illuminate\Database\Connection; | |
use Illuminate\Contracts\Routing\ResponseFactory; | |
/** | |
* A repository... | |
*/ | |
class PostRepository | |
{ | |
protected $connection; | |
public function __construct(Connection $connection) | |
{ | |
$this->connection = $connection; | |
} | |
public function all() | |
{ | |
return $this->connection->table('posts')->get(); | |
} | |
} | |
/** | |
* The responder... | |
*/ | |
class ListPostsResponder | |
{ | |
protected $response; | |
public function __construct(ResponseFactory $response) | |
{ | |
$this->response = $response; | |
} | |
public function handle(Collection $data) | |
{ | |
if (count($data) === 0) { | |
return $this->response->make('Not found.', 404); | |
} else { | |
return $this->response->view('posts.index', ['posts' => $data->all()]); | |
} | |
} | |
} | |
/** | |
* The action... | |
*/ | |
class ListPosts | |
{ | |
public function __invoke(Request $request, | |
ListPostsResponder $responder, | |
PostRepository $posts) | |
{ | |
return $responder->handle( | |
$posts->all() | |
); | |
} | |
} | |
/** | |
* The route... | |
*/ | |
$router->get('/posts', [ | |
'uses' => ListPosts::class | |
]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment