I am currently developing an plugin for zend-framework inspired by drupal versions 6 and 7 and for quite some time I ran into two difficulties that didn't seem to have a simple solution:
I needed to create routes that support case insensitive writes regardless of whether the server or host OS is case insensitive or not. This would ensure two things: a. OS independence and b. routing robustness when the parts of a route are variables that mask queue parameters of the type
?q=
(friendly addresses in drupal eg).When you have a correctly registered route eg
/ayudas/objetos
and you use/ayudas/objetos/
or need to pass it a parameter queue like/ayudas/objetos/clases/nombreclase/nombremiembro/etc
that masks something similar to/ayudas/objetos?tipo=clases&nombre=nombreclase&...
I get ERROR 404 page not found! or one of those non-existent controller or non-existent view outputs with the typical trace.
I reviewed, I think in depth, both the official zend-framework documentation and the related questions in various developer forums including SOen 's and the best I found were arguments explaining why it's done the way it's done. And, they repeat what you already know: how to name actions, controllers, files, and view template directories correctly. They add nothing of substance to the matter.
Furthermore: using the definition pattern normally (I tried at first) results in a plethora of almost identical program files, differing only by a couple of letters (sometimes the name) and scattered around a growing directory system. to respond to the naming model. Obviously seeing that made me allergic and made me feel like I was wasting my time.
I'm going to answer my own question to try to fill the gap I found when searching for help and documentation on this.
I am convinced that one learns more than how one thought to solve a problem? that of the solution itself. So I'm going to start by showing the approach: The problem was divided into three parts:
Finally, the solutions to the previous points must be collected in a class that allows them to be available to define and register routers for applications and extension modules based on zend . And, to verify the solution: Start the script of two plugins using it to register your routing system.
Basic route declaration model in Zendframework 3x
The routes are usually declared in the file
module/Nombre_del_Modulo/config/module.config.php
under the key'router'
. Although, I like to define them in a separate file to facilitate their review and maintenance, so in the config module I replace the preference definition subarrays with links to the files that contain them:and in the routes declaration file:
As you can see it is declared
use Zend\Router\Http\Segment;
to make use of the segment route type that makes it easy to declare discrete urls (by sections between directory separators with nested routes). And, the abstract class is declared that will offer the methods that make it easy to declare routes with the desired conditions.'constraints'
Let us now see the route definition mechanism by assigning:
The path must start with
/
and must contain the replacement string, which is specified by a regular expression in constraints for:ayudas
. It may or may not be followed by/p
and the value determined by the regular expression that is assigned to:pagina
or by the optional similar group/
followed by:letra
and optionally followed by/p
and:alfapagina
.As you can see in the code (above) the regular expressions for the replacement positions are set in the class
Router2ls
Declaration of a case-insensitive route
The assignment
'ayudas' => Router2ls::freeCaseExt('ayudas')
invokes a method that generates a regular expression so that all possible spellings of the word help are admitted, so that /help, /help, etc., are interpreted as the same path. And$page_constraint
it is a commonly used array, which defines in a standardized way the regular expressions for routes that need to have automatic pagination. eg /Ayudas/E/p45 requests page 45 of the helps with a title that starts with E, while /Ayudas/p45 requests page 45 of the general list of helps.Note that it
Router2ls::PaginateConstraints();
calls a static method of an abstract class; just likeRouter2ls::freeCaseExt('ayudas')
.I'm not going to show the whole class. Only the methods that solve the annotated question:
Defining a multilevel route
As you can see, the class
Router2ls
has a method prepared to hang multilevel routes (10 levels deep) when configuring any module. I will illustrate how to use it under the route/ayudas
so that all strings of the form are admittedThis is /helps/object with any script and up to nine optional parameters:
Obviously, in the module's src/Controller folder there must be a class
AyudasController
that extends the classZend\Mvc\Controller\AbstractActionController
and contains at least two public methods indexAction() and objectsAction() and their associated views.I hope it is useful.