Try to replace Exception
by ResourceNotFoundException
. And don't forget use
statement:
<?php
namespace OOOO\AdvertisingBundle\Utilities;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\Router;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
class RouteConverter
{
private $router;
public function __construct(Router $router)
{
$this->router = $router;
}
public function uriToRoute($uri)
{
try {
$matched = $this->router->match($uri);
} catch (ResourceNotFoundException $e) {
return null;
}
$result['name'] = $matched['_route'];
unset($matched['_route']);
unset($matched['_controller']);
$result['parameters'] = $matched;
return $result;
}
public function generateRoute($name, $parameters)
{
try {
$matched = $this->router->generate($name, $parameters, UrlGeneratorInterface::RELATIVE_PATH);
} catch (ResourceNotFoundException $e) {
return null;
}
return $matched;
}
}
or if you want to use Exception
, don't forget slash before name in Symfony:
<?php
namespace OOOO\AdvertisingBundle\Utilities;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\Router;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
class RouteConverter
{
private $router;
public function __construct(Router $router)
{
$this->router = $router;
}
public function uriToRoute($uri)
{
try {
$matched = $this->router->match($uri);
} catch (\Exception $e) {
return null;
}
$result['name'] = $matched['_route'];
unset($matched['_route']);
unset($matched['_controller']);
$result['parameters'] = $matched;
return $result;
}
public function generateRoute($name, $parameters)
{
try {
$matched = $this->router->generate($name, $parameters, UrlGeneratorInterface::RELATIVE_PATH);
} catch (\Exception $e) {
return null;
}
return $matched;
}
}