Skip to content

Instantly share code, notes, and snippets.

@dg
Created May 31, 2018 14:00
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dg/c4f323f8be94b2bf0ea47bcd4a42d899 to your computer and use it in GitHub Desktop.
Save dg/c4f323f8be94b2bf0ea47bcd4a42d899 to your computer and use it in GitHub Desktop.
Composing presenters without inheritance
<?php
// composing presenters without inheritance
// (requires nette/application 2.4.11)
trait RequireLoggedUser
{
public function injectRequireLoggedUser()
{
$this->onStartup[] = function () {
if (!$this->getUser()->isLoggedIn()) {
$this->redirect('Sign:in', $this->storeRequest());
}
};
}
}
trait AdminLayout
{
public function injectLayout(Model $model)
{
$this->onStartup[] = function () use ($model) {
$this->layout = 'admin.layout';
$this->template->menu = $model->getMenu();
};
}
}
class DashboardPresenter extends Nette\Application\UI\Presenter
{
use RequireLoggedUser;
use AdminLayout;
}
class SignPresenter extends Nette\Application\UI\Presenter
{
use AdminLayout;
}
@enumag
Copy link

enumag commented Jun 1, 2018

My solution would be marker interfaces or doctrine annotations on presenters and Symfony subscribers (using contributte dispatcher) to handle it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment