0

Using the CMF code as a reference I am trying to replace the default router with the chain router...not just decorate...

public function process(ContainerBuilder $container)
{
    // NOT available using alias for some reason???
    $definition = $container->getDefinition('MyBundle\Routing\ChainRouter');

    foreach ($container->findTaggedServiceIds($this->routerTag) as $id => $attributes) {
        $priority = isset($attributes[0]['priority']) ? $attributes[0]['priority'] : 0;
        $definition->addMethodCall('add', [new Reference($id), $priority]);
    }

    $container->setDefinition('router.default', $definition);
}

Here is the bundle services.xml

 <services>

        <service id="MyBundle\Routing\ChainRouter" />
        <service id="cmf_routing.router" alias="MyBundle\Routing\ChainRouter" />

<!--        <service id="MyBundle\Routing\DynamicRouter">-->
<!--            <tag name="router" priority="100" />-->
<!--        </service>-->

        <service id="Symfony\Bundle\FrameworkBundle\Routing\Router">
            <tag name="router" priority="1" />

            <tag name="monolog.logger" channel="router" />
            <tag name="container.service_subscriber" id="routing.loader" />
            <argument type="service" id="Psr\Container\ContainerInterface" />
            <argument>%router.resource%</argument>
            <argument type="collection">
                <argument key="cache_dir">%kernel.cache_dir%</argument>
                <argument key="debug">%kernel.debug%</argument>
                <argument key="generator_class">Symfony\Component\Routing\Generator\CompiledUrlGenerator</argument>
                <argument key="generator_dumper_class">Symfony\Component\Routing\Generator\Dumper\CompiledUrlGeneratorDumper</argument>
                <argument key="matcher_class">Symfony\Bundle\FrameworkBundle\Routing\RedirectableCompiledUrlMatcher</argument>
                <argument key="matcher_dumper_class">Symfony\Component\Routing\Matcher\Dumper\CompiledUrlMatcherDumper</argument>
            </argument>
            <argument type="service" id="router.request_context" on-invalid="ignore" />
            <argument type="service" id="parameter_bag" on-invalid="ignore" />
            <argument type="service" id="logger" on-invalid="ignore" />
            <argument>%kernel.default_locale%</argument>
            <call method="setConfigCacheFactory">
                <argument type="service" id="config_cache_factory" />
            </call>
        </service>

I am getting an error Cannot load resource "kernel::loadRoutes"

With the following stack trace

LoaderLoadException Symfony\Component\Config\Exception\LoaderLoadException: Cannot load resource "kernel::loadRoutes".

  at vendor/symfony/config/Loader/DelegatingLoader.php:37   at Symfony\Component\Config\Loader\DelegatingLoader->load('kernel::loadRoutes', null)
     (vendor/symfony/framework-bundle/Routing/DelegatingLoader.php:67)   at Symfony\Bundle\FrameworkBundle\Routing\DelegatingLoader->load('kernel::loadRoutes', null)
     (vendor/symfony/framework-bundle/Routing/Router.php:66)   at Symfony\Bundle\FrameworkBundle\Routing\Router->getRouteCollection()
     (vendor/symfony/routing/Router.php:358)   at Symfony\Component\Routing\Router->getMatcherDumperInstance()
     (vendor/symfony/routing/Router.php:289)   at Symfony\Component\Routing\Router->Symfony\Component\Routing\{closure}(object(ResourceCheckerConfigCache))
     (vendor/symfony/config/ResourceCheckerConfigCacheFactory.php:39)   at Symfony\Component\Config\ResourceCheckerConfigCacheFactory->cache('/vagrant/project/var/cache/dev/url_matching_routes.php', object(Closure))
     (vendor/symfony/routing/Router.php:297)   at Symfony\Component\Routing\Router->getMatcher()
     (vendor/symfony/routing/Router.php:251)   at Symfony\Component\Routing\Router->matchRequest(object(Request))
     (/vagrant/bundles/mybundle/src/Routing/ChainRouter.php:191)   at MyBundle\Routing\ChainRouter->doMatch('/', object(Request))
     (/vagrant/bundles/mybundle/src/Routing/ChainRouter.php:158)   at MyBundle\Routing\ChainRouter->matchRequest(object(Request))
     (vendor/symfony/http-kernel/EventListener/RouterListener.php:112)

truncated for brevity...

I feel this is being caused by the default router being "replaced" in the compiler pass and not properly re-initialized. Seeing as the DynamicRouter isn't even loaded the expected behavior was business as usual, not this error :)

Any thoughts, pointers, tips, suggestions?

Alex.Barylski
  • 2,843
  • 4
  • 45
  • 68
  • Is there any good reason to replace and not decorate the common router? – Nico Haase Mar 06 '20 at 16:12
  • Well...replacement is needed (I assume) because I am not enhancing the default router, rather replacing it. It then gets added as priority zero to a chain of available routers, with the CMF dynamic router being second...make sense? Oddly I just read a comment that Symfony deprecated service replacement but can't find confirmation on any site... – Alex.Barylski Mar 06 '20 at 16:33
  • Haven't heard that before - where's the source that you cannot override services anymore? That would cause many implementation problems – Nico Haase Mar 06 '20 at 19:08
  • Oh god...it was just a comment I read on some other post on here. Nothing else since then, so maybe someone spoke to soon? :p I am ignoring that suggestion and my own statement lol – Alex.Barylski Mar 06 '20 at 19:15
  • @NicoHaase any thoughts on what I might be doing wrong? At this point I'd be happy just replacing original service and adding to a chain, effectively the same thing as decorating, but with chaining instead... – Alex.Barylski Mar 06 '20 at 20:05
  • Sorry, I have no clue. Overriding a service would probably work, but I don't know why you would need all that other stuff you wrote about – Nico Haase Mar 06 '20 at 20:07
  • Let me ask you this...why would over-writing the service, as I've done, stomp on the routes? I configure the new ChainRouter service identically to how Symfony does it - I copied their code verbatim for the services.xml!?!? – Alex.Barylski Mar 06 '20 at 20:14
  • Even decorating the service and copying the Symfony routing services.xml gives me the same error...uggh – Alex.Barylski Mar 06 '20 at 22:32
  • I am getting the same error... Havey ou figured out yet? @Alex.Barylski – Microtribute Sep 02 '20 at 07:41

0 Answers0