Skip to content

Instantly share code, notes, and snippets.

@bdunogier
Last active October 12, 2015 07:21
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 bdunogier/952d297908094caf3648 to your computer and use it in GitHub Desktop.
Save bdunogier/952d297908094caf3648 to your computer and use it in GitHub Desktop.
Custom eZ Platform actions, before and after
<?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;
}
}
@andrerom
Copy link

Better if user service was injected so it would be $this->userService->loadUser( ... )

Better for: perf, readability, clear dependencies, ..

@andrerom
Copy link

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.

@bdunogier
Copy link
Author

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.

@bdunogier
Copy link
Author

Updated usage of the UserService. It is pseudo code, let's say it is injected ;-)

@lolautruche
Copy link

Did you forget the after file?

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