Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/252.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony2可以';无法捕获ResourceNotFoundException_Php_Symfony_Exception_Routing - Fatal编程技术网

Php Symfony2可以';无法捕获ResourceNotFoundException

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转换为带有参数的RouteName的服务
我试图捕获不存在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;
    }
}