I have a service that looks up data for a page, but if that data isn't found, should redirect to the homepage. For the life of me, I can't figure out how to do this in Sf2. There are so many different ways to work with services and router, but none seem to work.
namespace Acme\SomeBundle\Services;
use Acme\SomeBundle\Entity\Node;
use \Doctrine\ORM\EntityManager;
use \Symfony\Component\HttpKernel\Event\GetResponseEvent;
use \Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use \Symfony\Bundle\FrameworkBundle\Routing\Router;
use \Symfony\Component\Routing\Generator\UrlGenerator;
use Symfony\Component\HttpFoundation\RedirectResponse;
class NodeFinder
{
private $em;
private $router;
public function __construct(EntityManager $em, Router $router)
{
$this->em = $em;
$this->router = $router;
}
public function getNode($slug)
{
$node = $this->em->getRepository('SomeBundle:Node')->findOneBy(array('slug' => $slug));
if (!$node) { //if no node found
return $this->router->redirect('homepage', array(), true);
}
}