php - Symfony2 / FOSUserBundle: Change render variables in response without altering parent class -
one of classes extends basecontroller on fosuserbundle, , returns parent action. however, due project spec, shouldn't have need edit parent class. there way of sending additional variables, twig render, through child response?
child class:
class changepasswordcontroller extends basecontroller { public function changepasswordaction(request $request) { $response = parent::changepasswordaction($request); return $response; // , 'myvariable' => $myvariable } } parent class:
class changepasswordcontroller extends containeraware { /** * change user password */ public function changepasswordaction(request $request) { //lots of code..... return $this->container->get('templating') ->renderresponse( 'fosuserbundle:changepassword:changepassword.html.' .$this->container->getparameter('fos_user.template.engine'), array( 'form' => $form->createview() //and 'myvariable' => $myvariable ) ); } } so summarise, there way of passing parent class, without changing parent class... whilst rendering twig view additional variable.
-- update --
essentially want render form using fosuserbundle changepassword action, therefore works fine:
return $this->container ->get('templating') ->renderresponse( 'fosuserbundle:changepassword:changepassword.html.'.$this->container->getparameter('fos_user.template.engine'), array('form' => $form->createview()) ); however, want pass more variables view, 'form' passed shown above, without altering fosuserbundle changepassword controller. therefore have class inherits controller, adds additional functionality , returns parent change password action:
class changepasscontroller extends changepasswordcontroller { public function changepasswordaction(request $request) { // more code...... $response = parent::changepasswordaction($request); return $response; } } but, applications, want add more form variable view template. there way of passing additional variable view, without altering parent controller / action? (but not like) pushing 'myvariable' => $myvariable parent changepasswordaction return statement?
there section in fosuserbundle documentation describes how that, , symfony2's cookbook, how use bundle inheritance override parts of bundle.
in summary, create bundle class override fosuserbundle in src:
// src/acme/userbundle/acmeuserbundle.php <?php namespace acme\userbundle; use symfony\component\httpkernel\bundle\bundle; class acmeuserbundle extends bundle { public function getparent() { return 'fosuserbundle'; } } then, override changepasswordcontroller class:
use fos\userbundle\controller\changepasswordcontroller basecontroller; class changepasswordcontroller extends basecontroller { public function changepasswordaction(request $request) { $response = parent::changepasswordaction($request); return $response; // , 'myvariable' => $myvariable } } --update--
ok think misread question. anyway renderresponse() of templating service essentially:
$response->setcontent($this->render($view, $parameters)); you can see class of templating service running app/console container:debug twigengine class.
so can re-invoke renderresponse() , supply own parameters. eg:
return $this->container->get('templating')->renderresponse( 'fosuserbundle:changepassword:changepassword.html.'.$this->container->getparameter('fos_user.template.engine'), array( 'form' => $form->createview(), 'myvariable' => $myvariable', // there go ), $response // previous response has been rendered parent class, not necessary );
Comments
Post a Comment