Skip to content

Instantly share code, notes, and snippets.

@mattattui
Created July 20, 2011 17:27
Show Gist options
  • Save mattattui/1095423 to your computer and use it in GitHub Desktop.
Save mattattui/1095423 to your computer and use it in GitHub Desktop.
Sample Object Route Extension
services:
JobRoute.twig.extension:
class: Me\MyBundle\Extension\JobRouteExtension
arguments: [@router]
tags:
- { name: twig.extension }
<?php
namespace Me\MyBundle\Extension;
use Me\MyBundle\Entity\Job;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class JobRouteExtension extends \Twig_Extension
{
private $generator;
public function __construct(UrlGeneratorInterface $generator)
{
$this->generator = $generator;
}
public function getFunctions() {
return array(
'job_url' => new \Twig_Filter_Method($this, 'job_url'),
);
}
public function job_url(Job $job)
{
$name = 'job_show';
$parameters = array(
'company' => $job->getCompanySlug(),
'location' => $job->getLocationSlug(),
'id' => $job->getId(),
'position' => $job->getPositionSlug(),
)
return $this->generator->generate($name, $parameters, true);
}
public function getName()
{
return 'job_route';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment