Last active
April 2, 2018 11:49
-
-
Save KaiCMueller/9e134036395bd782d4ad3934ebe8bc93 to your computer and use it in GitHub Desktop.
Add template parameters in EasyAdmin
This file contains 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 | |
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); | |
} | |
} |
This file contains 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 | |
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