Skip to content

Instantly share code, notes, and snippets.

@KaiCMueller
Last active April 2, 2018 11:49
Show Gist options
  • Save KaiCMueller/9e134036395bd782d4ad3934ebe8bc93 to your computer and use it in GitHub Desktop.
Save KaiCMueller/9e134036395bd782d4ad3934ebe8bc93 to your computer and use it in GitHub Desktop.
Add template parameters in EasyAdmin
<?php
namespace App\Controller;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AdminController as BaseAdminController;
/**
* General controller for administrating all entities
*/
class AdminController extends BaseAdminController
{
/**
* Used to add/modify/remove parameters before passing them to the Twig template.
* If the controller implements an action specific method (e.g. renderEditTemplate)
* it will be used
*
* @param string $actionName The name of the current action (list, show, new, etc.)
* @param string $templatePath The path of the Twig template to render
* @param array $parameters The parameters passed to the template
*
* @return \Symfony\Component\HttpFoundation\Response
*/
protected function renderTemplate($actionName, $templatePath, array $parameters = [])
{
$customRenderMethod = 'render' . ucfirst($actionName) . 'Template';
if (method_exists($this, $customRenderMethod)) {
return $this->$customRenderMethod($actionName, $templatePath, $parameters);
}
return $this->render($templatePath, $parameters);
}
}
<?php
namespace App\Controller;
use App\Controller\AdminController as BaseAdminController;
class CustomController extends BaseAdminController
{
/**
* Used to add/modify/remove parameters before passing them to the Twig template.
* Instead of defining a render method per action (list, show, search, etc.) use
* the $actionName argument to discriminate between actions.
*
* @param string $actionName The name of the current action (list, show, new, etc.)
* @param string $templatePath The path of the Twig template to render
* @param array $parameters The parameters passed to the template
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function renderEditTemplate($actionName, $templatePath, array $parameters = [])
{
$parameters['myCustomParameter'] = 'Custom Value';
return $this->render($templatePath, $parameters);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment