Question

I created a service which converts uris to routenames with parameters
I'm trying to catch the ResourceNotFoundException for not existing uris but getting 500 Error and exception

<?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;
    }
}
Was it helpful?

Solution

Try to replace Exception by ResourceNotFoundException. And don't forget use statement:

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

or if you want to use Exception, don't forget slash before name in 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;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top