2

I've written a custom Part class (MyNamespace\Mvc\Router\Http\Part), that shall now be used instead instead of the Zend\Mvc\Router\Http\Part. How to load it to the list of the invokables of the Zend\Mvc\Router\RoutePluginManager?

enter image description here

--

EDIT:

Just tried it with

namespace Application;

use Zend\Mvc\MvcEvent;

class Module {
    ...
    public function onBootstrap(MvcEvent $mvcEvent) {
        ...
        $routePluginManager = $mvcEvent->getRouter()->getRoutePluginManager();
        $routePluginManager->setInvokableClass('part', 'MyWorkspace\Mvc\Router\Http\Part');
        ...
    }
    ...
}

but it's not working -- the standard Zend class is still used.

tshepang
  • 12,111
  • 21
  • 91
  • 136
automatix
  • 14,018
  • 26
  • 105
  • 230

1 Answers1

1

Either have your Module class implement the Zend\ModuleManager\Feature\RouteProviderInterface and declare the getRouteConfig method it requires

<?php
namespace Application;

use Zend\ModuleManager\Feature;

class Module implements Feature\RouteProviderInterface
{

    public function getRouteConfig()
    {
        return array(
            'invokables' => array(
                 'part' => 'ITT\Mvc\Router\Http\Part',
            ),
        );
    }

 }

Or, add it to your module.config.php file under the route_manager key

 return array(

     'route_manager' => array(
         'invokables' => array(
             'part' => 'ITT\Mvc\Router\Http\Part',
          ),
      ),

  );
Crisp
  • 11,417
  • 3
  • 38
  • 41
  • 1
    Thank you for your answer! Neither the variant with the `getRouteConfig()`, nor the one with the `module.config.php` is working for me. The class is loaded in to the `PoutePluginManager`, but then the `Zend\Mvc\Router\Http\Part#match(...)` called instead of `MyNamespace\Mvc\Router\Http\Part#match(...)`. – automatix Oct 13 '13 at 12:37
  • 1
    A took a look into the [`Zend\Mvc\Router\Http\TreeRouteStack#init()`](https://github.com/weierophinney/Component_ZendMvc/blob/master/Router/Http/TreeRouteStack.php#L43). It's the place, where the route plugin manager gets its invokables. Both classes are loaded -- as the Zend one as well mine. They have different canonical names (`RoutePluginManager#canonicalNames`). My suspicion is, that my custom class is "ignored", because the object, that calls the `Path#match(...)` searchs in the list of invokables by canonical name `zendmvcrouterhttppart`. – automatix Oct 13 '13 at 13:06
  • 1
    The instantiating if the `Part` class takes place in the `Zend\Mvc[\Router\RoutePluginManager#setInvokableClass(...)`](https://github.com/zendframework/zf2/blob/master/library/Zend/Mvc/Router/RoutePluginManager.php#L43). It gets the canonical name "part" and looks up in the invokables array. And my custoom class is not there. I use another custom class, that works (a custom route `MyNamespace\Mvc\Router\Http\UnicodeRegex`), but it's also not there. There are no custom classes there at all. The value of the "part" is the Zend `Part` class and it is instantited. – automatix Oct 13 '13 at 13:27
  • 1
    OK, I found out, what the problem is: The [`Zend\Mvc\Router\Http\TreeRouteStack#init()`](https://github.com/zendframework/zf2/blob/master/library/Zend/Mvc/Router/Http/TreeRouteStack.php#L79) overrides the route config. It iterates an internal array and calls the `RoutePluginManager#setInvokableClass(...)` with the arguments: `$name` = the canonical name (in our case "part") and `$class` = `__NAMESPACE__ . '\ClassName'` (in our case `__NAMESPACE__ . '\Part'`). `__NAMESPACE__` is `Zend\Mvc\Router\Http` -- also the invokable class is `Zend\Mvc\Router\Http\Part`. Is it a bug? How to resolve it? – automatix Oct 13 '13 at 13:47
  • Probably little late to the party. Well I had the same "issue" but thanks to above I knew where to look. Lucky me I got more "issues" with zf2 so I made my own fork. In the `init` of `TreeRouteStack` where `setInvokableClass` is called, I placed an `if` around it with `!$routes->has($name)`. And now it works – MKroeders Oct 19 '14 at 20:18