-
-
Save bdunogier/952d297908094caf3648 to your computer and use it in GitHub Desktop.
<?php | |
class DemoController | |
{ | |
public function showBlogPostAction(Location $location, $viewType, $layout = false, array $params = array()) | |
{ | |
// We need the author, whatever the view type is. | |
$author = $this->userService->loadUser($location->getContentInfo()->ownerId); | |
// TODO once the keyword service is available, load the number of keyword for each keyword | |
// Delegate view rendering to the original ViewController | |
// (makes it possible to continue using defined template rules) | |
// We just add "author" to the list of variables exposed to the final template | |
return $this->get('ez_content')->viewLocation( | |
$location->id, | |
$viewType, | |
$layout, | |
array('author' => $author) + $params | |
); | |
} | |
} |
<?php | |
class DemoController | |
{ | |
public function showBlogPostAction(ContentView $view) | |
{ | |
$author = $this->userService()->loadUser($view->getContent()->contentInfo->ownerId); | |
$view->addParameters(['author' => $author]); | |
return $view; | |
} | |
} |
Also, this is nitpicking, but we should be better at naming (like anyone), calling this Action
is a bit misleading, and so is Controller
. Maybe we need to be more explicit in calling this ViewController
and maybe also ViewActions
or whatever you would like to call it :)
With this it would be much easier to glance at this and somehow understand what it is.
With this it would be much easier to glance at this and somehow understand what it is.
I wonder. I have a feeling that it is quite common to handle the View => Response conversion with a listener. Proof is, there is a kernel event for that, KernelEvents::VIEW
, that applies when a controller action returns something that isn't a Response
. The fact that there is a high level dedicated extension shows that it is a known approach.
For instance, it is what FOSRESTBundle and others use.
Updated usage of the UserService
. It is pseudo code, let's say it is injected ;-)
Did you forget the after file?
Better if user service was injected so it would be
$this->userService->loadUser( ... )
Better for: perf, readability, clear dependencies, ..