문제

Question is why

$config = array(__DIR__ . '/app/config');
$locator = new Symfony\Component\Config\FileLocator($config);
$loader = new Symfony\Component\Routing\Loader\YamlFileLoader($locator);
$routes_collection = $loader->load('routes.yml');

Here $routes_collection is an instance of Symfony\Component\Routing\RouteCollection, and this code works fine.

And here

# config.yml
file_locator:
    class:    Symfony\Component\Config\FileLocator
    arguments:  ['%app_root%/app/config']

route_collection:
    class:    Symfony\Component\Routing\Loader\YamlFileLoader
    arguments: ["@file_locator"]
    calls:
              - [load, ['routes.yml']]

#app.php
$routes_collection = $container->get('route_collection')

$routes_collection is an instance of Symfony\Component\Routing\Loader\YamlFileLoader and if i use it with Symfony\Component\Routing\Matcher\UrlMatcher am getting:

Argument 1 passed to Symfony\Component\Routing\Matcher\UrlMatcher::__construct() must be an instance of Symfony\Component\Routing\RouteCollection, instance of Symfony\Component\Routing\Loader\YamlFileLoader given.

Update

@Pazi ツ, but how to be if i want to use route_collection in matcher

matcher:
    class:    Symfony\Component\Routing\Matcher\UrlMatcher
    arguments:  ["@route_collection", "@context"]
도움이 되었습니까?

해결책 2

Of course your service is an instance of YamlFileLoader because you configured this in the class attribute. The return value of the methods calls doesn't influence the instance type. You have to call the method in php, if you want to get the return value.

# config.yml
file_locator:
    class:    Symfony\Component\Config\FileLocator
    arguments:  ['%app_root%/app/config']

yaml_file_loader:
    class:    Symfony\Component\Routing\Loader\YamlFileLoader
    arguments: ["@file_locator"]

#app.php
$routes_collection = $container->get('yaml_file_loader')->load('routes.yml');

다른 팁

If you want to take route_collection via config you need to define route_collection yaml_file_loader as the factory service of route_collection:

# config.yml
file_locator:
  class:    Symfony\Component\Config\FileLocator
  arguments:  ['%app_root%/app/config']

yaml_file_loader:
  class: Symfony\Copmonent\Routing\Loader\YamlFileLoader
  arguments: [ "@file_locator" ]

route_collection:
  class: Symfony\Component\Routing\RouteCollection
  factory_service: yaml_file_loader
  factory_method: load
  arguments: [ 'routes.yml' ]

This way you can do

$routes_collection = $container->get('route_collection');
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top