On our main symfony2 application at work I have had to manipulate some outputted grid fields by wrapping them in a hyperlink tag. This happens in quite a few different controllers, so I made a method to do this to prevent code repetition.
I decided to do this using DOMDocument and construct the new hyperlink with that.
public function linkWrap($string, $route, array $route_params = [], $prefilter = null)
{
if(strlen($string) > 0)
{
$dom = new \DOMDocument();
$new_hyper = $dom->createElement('a');
$new_hyper->setAttribute('href', $this->generateUrl($route, $route_params).(isset($prefilter) ? '?'.htmlspecialchars($prefilter) : ''));
$new_hyper->nodeValue = $string;
return $dom->saveHTML($new_hyper);
}
return $string;
}
The method takes a number of options the last of which is used only to append a prefilter query string for use with APYGridBundle.
My question is, does this cause a significantly larger overhead than just constructing the link in a more 'conventional' way?
As follows:
$link = "<a href=" . $this->generateUrl('a_route', array('id' => 23)) . '?'.$prefilter">some text</a>";
In addition, any tips on how any of could be improved would be appreciated.