Tuesday, 10 September 2013

Add the same calculated property to every Symfony2 Controller in bundle

Add the same calculated property to every Symfony2 Controller in bundle

I'm converting an application over from a legacy framework to Symfony2.
One of the things the app has is a tiny little message in the bottom
corner of the rendered page that says
served by appserver3 in 200ms
or similar. It changes depending on the appserver you're on and the amount
of time it took to serve the request. The value for the appserver is set
in
Should I create a BaseController type class that extends Controller and
then have all my controllers extend that? If so, how would I go about
making sure that the BaseController always adds the responseTime and
appServerName to the list of variables that will be passed to my twig
template without having to add it manually in every controller like this:
return $this->render('MyBundle:Default:index.html.twig', array('var1' =>
$var1, 'appServerName' => $this->getAppServerName(), 'responseTime' =>
$this->getResponseTime()));
(Obviously this would assume that I created the getAppServerName and
getResponseTime methods in my BaseController.)
This seems like a lot of copypasta and my whole reason for moving to
Symfony2 is to avoid that :\

No comments:

Post a Comment