Symfony2无法捕获ResourceNotFoundException

时间:2014-02-21 17:49:19

标签: php symfony exception routing

我创建了一个服务,它将uris转换为带有参数
的路径名 我正在尝试捕获ResourceNotFoundException以查找不存在的uris但获得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;
    }
}

1 个答案:

答案 0 :(得分:2)

尝试将Exception替换为ResourceNotFoundException。不要忘记use声明:

<?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;
    }
}

或者如果你想使用Exception,请不要忘记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;
    }
}