繁体   English   中英

Symfony2无法捕获ResourceNotFoundException

[英]Symfony2 can't catch ResourceNotFoundException

我创建了一个服务,将uris转换为带参数的routenames
我正在尝试捕获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;
    }
}

尝试用ResourceNotFoundException替换Exception 并且不要忘记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;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM