0
votes

I have a big ZF2 project using a lot of modules.

We need to find a way to automatically PREPEND an Optional custom route to every existing route in all modules, using code from only One module.

And it needs to work with the Url view helper.

In every module I have the default route which is basically : /ModuleName[/:controller[/:action]]

But my new module (Company) needs to add a Company context to every route like so : [/company/:company_id] /ModuleName[/:controller[/:action]]

As you may have understood, the /company/id part is Optional, and I need a default company_id.

I do not want to add this route in every module's config file.

I tried adding a route in the Company module's config file, but I quickly understood that we cannot define the MODULE parameter inside the route definition, since Zend2 uses namespaces instead.

Also, we will need to do the same thing for langs.

At the end, we will need something like this :

[/:lang][/company/:company_id]/ModuleName[/:controller[/:action]]

Without changing the current config files in any other existing module.

Anyone has The solution for this ?

thank you !

1
zendexperts.com/2012/12/09/custom-routing-in-zend-framework-2 you can inmplement the routing interface and add some logic to your routes.cptnk

1 Answers

0
votes

I have solved my problem, and will share it here if anyone needs it.

I will only explain the big idea without posting any line of code since it strongly depends on the way you built your project, and also, it is better to implement it your own way to understand it better, cause if you copy-paste it without trying to understand it WILL break your whole project. That said, if you cannot understand this explanation, you cannot implement this anyways.

First of all, I have a BaseModule that every module class extends from, and this BaseModule implements the getConfig() method itself automatically and it is final so no module can override it. If your project is not built like that it will NOT work and you can stop right here.

In this BaseModule I have a STATIC method called prependRouteSegment(), which basically adds key=>array to a PRIVATE STATIC array variable. In my case, the key is the route and the array is the default params for this route.

Then, in the getConfig() method, after fetching the config file and before returning it as the config array, I can edit anything I want. So basically I edit all the existing 'Segment' routes and concatenate my routes from the static variable and the original one, then I add my default params.

But there is one more thing that is very important, you need to actually set the custom routes from each Module BEFORE zend executes every module's getConfig() method.

To do so, first create a STATIC method initCustomRouting() that you can override in any module, then, say my first loaded module is Application, simply parse the list of modules in the __construct() method of your Application module and call this initCustomRouting() statically on each module.

Then, if a certain module needs to prepend a custom route to any existing route, you simply implement in this module the initCustomRouting() method and inside of it you call the prependRouteSegment() static method with your custom route and default params.

That's it !

Dont forget that you cannot use params in a Literal route, and you may not want to prepend something on a hostname route, so in my case I only add the routes to every 'Segment' routes, I simply compare the type when I loop through.

Hope this will help someone !