2014-02-21 60 views
0

我創建了轉換URI來routenames與參數
我想搭上不存在的URI,但得到500錯誤和異常Symfony2中不能趕上ResourceNotFoundException

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

你能告訴你的php日誌?你不應該使用異常,所以你應該捕獲(\ Exception $ e) – ziollek

回答

2

嘗試更換ResourceNotFoundException服務Exception by 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; 
    } 
} 
+0

異常是通用的,它捕獲所有異常 – ziollek

+0

@ziollek只有在'ResourceNotFoundException ** **擴展**異常' –

+0

所有異常必須是Exception類或Exception的子類。請閱讀說明書:http://pl1.php.net/exceptions – ziollek