Created
October 15, 2014 12:42
-
-
Save danharper/2a6aaa43ae64eb577e0e to your computer and use it in GitHub Desktop.
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 | |
// Version A: Facades everywhere - typical Laravel 4.2 style | |
// routes.php | |
Route::get('payments', 'BillingController@recentPayments'); | |
// BillingController.php | |
class BillingController { | |
public function __construct(PaymentsRepository $payments) | |
{ | |
$this->payments = $payments; | |
} | |
public function recentPayments() | |
{ | |
return View::make('billing.payments', [ | |
'payments' => $this->payments->recent(); | |
]); | |
} | |
} |
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 | |
// Version B: No facades, dependency injection | |
// routes.php | |
$router->get('payments', 'BillingController@recentPayments'); | |
// BillingController.php | |
class BillingController { | |
public function __construct(PaymentsRepository $payments, Factory $view) | |
{ | |
$this->payments = $payments; | |
$this->view = $view; | |
} | |
public function recentPayments() | |
{ | |
return $this->view->make('billing.recent-payments', [ | |
'payments' => $this->payments->recent(); | |
]); | |
} | |
} |
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 | |
// Version C: Routing annotations | |
class BillingController { | |
public function __construct(PaymentsRepository $payments, Factory $view) | |
{ | |
$this->payments = $payments; | |
$this->view = $view; | |
} | |
/** | |
* @Get("/payments") | |
*/ | |
public function recentPayments() | |
{ | |
return $this->view->make('billing.recent-payments', [ | |
'payments' => $this->payments->recent(); | |
]); | |
} | |
} |
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 | |
// Version D: Annotations for views, instead of injecting the view factory (or using the facade) | |
class BillingController { | |
public function __construct(PaymentsRepository $payments) | |
{ | |
$this->payments = $payments; | |
} | |
/** | |
* @Get("/payments") | |
* @View("recent-payments") | |
*/ | |
public function recentPayments() | |
{ | |
return [ | |
'payments' => $this->payments->recent(); | |
]; | |
} | |
} |
Nice.
But I don't get how the view()
function is any improvement over the "facades". Only case I see for them is to remove the potential confusion of having to use \View::make()
instead of View::make()
inside the now-namespaced controllers.
I'm not sure a @View("..")
annotation is a good thing either, though. Mostly just been hacking about to see how easy custom Annotations are (they're kinda not).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Version E - Laravel 5.0 Style:
How's that?