【问题标题】:Symfony2 can't catch ResourceNotFoundExceptionSymfony2 无法捕获 ResourceNotFoundException
【发布时间】:2014-02-21 17:49:19
【问题描述】:

我创建了一个将 uri 转换为带参数的路由名称的服务
我试图捕获 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;
    }
}

【问题讨论】:

  • 你能显示你的php日志吗?你不使用异常,所以你应该 catch(\Exception $e)

标签: php symfony exception routing


【解决方案1】:

尝试将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;
    }
}

【讨论】:

  • 异常是通用的,它会捕获所有异常
  • @ziollek 仅适用于ResourceNotFoundException 扩展 Exception
  • 所有异常必须是 Exception 类的实例或 Exception 的子类。请阅读手册:pl1.php.net/exceptions
猜你喜欢
  • 1970-01-01
  • 2021-09-04
  • 2014-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-08
  • 2012-11-09
  • 2011-03-14
相关资源
最近更新 更多