Last active
October 12, 2015 07:21
-
-
Save bdunogier/952d297908094caf3648 to your computer and use it in GitHub Desktop.
Custom eZ Platform actions, before and after
This file contains hidden or 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 | |
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 | |
); | |
} | |
} |
This file contains hidden or 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 | |
class DemoController | |
{ | |
public function showBlogPostAction(ContentView $view) | |
{ | |
$author = $this->userService()->loadUser($view->getContent()->contentInfo->ownerId); | |
$view->addParameters(['author' => $author]); | |
return $view; | |
} | |
} |
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?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Also, this is nitpicking, but we should be better at naming (like anyone), calling this
Action
is a bit misleading, and so isController
. Maybe we need to be more explicit in calling thisViewController
and maybe alsoViewActions
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.