Создание собственной структуры на основе Sf2-компрониентов, и я пытаюсь создать услугу маршрутизатора.Создание настраиваемого сервиса маршрутизатора
я я нужен этот сервис для generateUrl()
метода
protected function generateUrl($route, $parameters = array(), $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH)
{
return $this->get('router')->generate($route, $parameters, $referenceType);
}
Я стараюсь это
$container = new ContainerBuilder();
$container->setDefinition('router_loader', new Definition('Symfony\Component\Config\Loader\LoaderInterface'));
$container->setDefinition('router', new Definition('Symfony\Component\Routing\Router', array()));
И когда я исполняю в моем methodAction
$this->generateUrl('home');
он вернулся со мной:
Catchable fatal error: Argument 1 passed to Symfony\Component\Routing\Router::__construct() must be an instance of Symfony\Component\Config\Loader\LoaderInterface, none given in D:\xampp\htdocs\my_fw\vendor\symfony\routing\Router.php on line 95
глядя на конструктор маршрутизатора, которого я вижу. Мне нужен этот интерфейс
public function __construct(LoaderInterface $loader, $resource, array $options = array(), RequestContext $context = null, LoggerInterface $logger = null)
Как избежать этой реализации в эксплуатации?
**New update:** routing.php
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;
// Routing
$routes = new RouteCollection();
// Home
$routes->add('home', new Route('/', array(
'_controller' => 'MyCompany\\Controller\\HomeController::indexAction',
)));
Имейте в виду, что класс Router - это все в одном классе типа менеджера с очень специфическими требованиями: http://symfony.com/doc/current/components/routing/introduction.html#the-all-in-one -router. Если вы не хотите или не нуждаетесь в такой сложности, просто используйте класс UrlGenerator напрямую: http://symfony.com/doc/current/components/routing/introduction.html#the-all-in-one-router – Cerad