Php Symfony2可以';无法捕获ResourceNotFoundException
我创建了一个将URI转换为带有参数的RouteName的服务Php Symfony2可以';无法捕获ResourceNotFoundException,php,symfony,exception,routing,Php,Symfony,Exception,Routing,我创建了一个将URI转换为带有参数的RouteName的服务 我试图捕获不存在URI的ResourceNotFoundException,但得到500个错误和异常 <?php namespace OOOO\AdvertisingBundle\Utilities; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Symfony\Component\Routing\Router; class Route
我试图捕获不存在URI的ResourceNotFoundException,但得到500个错误和异常
<?php
namespace OOOO\AdvertisingBundle\Utilities;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\Router;
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;
}
}
尝试用ResourceNotFoundException
替换异常。不要忘记使用语句:
<?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;
}
}
你能看看你的php日志吗?您不使用Exception,所以应该捕获(\Exception$e)Exception是泛型的,它可以捕获所有内容exceptions@ziollek仅当ResourceNotFoundException
扩展Exception
时,所有异常都必须是Exception类的实例或Exception的子类。请阅读手册:
<?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;
}
}