Skip to content

Instantly share code, notes, and snippets.

@danharper
Created October 15, 2014 12:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danharper/2a6aaa43ae64eb577e0e to your computer and use it in GitHub Desktop.
Save danharper/2a6aaa43ae64eb577e0e to your computer and use it in GitHub Desktop.
<?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();
]);
}
}
<?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();
]);
}
}
<?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();
]);
}
}
<?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();
];
}
}
@franzliedke
Copy link

Version E - Laravel 5.0 Style:

/**
 * @Get("/payments")
 */
public function recentPayments()
{
  return view('recent-payments', [
    'payments' => $this->payments->recent();
  ]);
}

How's that?

@danharper
Copy link
Author

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