-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Add controller namespace prefix to template mapping #5670
Add controller namespace prefix to template mapping #5670
Conversation
InjectTemplateListener changes behaviour when controller is matched by map entry. Mapped namespace prefix is replaced with provided value and rest of the controller inflected. If map entry value is boolean true, then whole controller class is inflected. In all cases\Controller\ namespace and trailing Controller are stripped. Most notable use case is when module name is not top level namespace With map entry 'Vendor\Module' => true and controller class Vendor\ModuleName\Controller\FooController resulting template name will be vendor/module-name/foo/action-name
continue; | ||
} | ||
|
||
if (is_string($map)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are there cases when it's not a string?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
boolean true
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, so this needs more documentation then.
Also, I'd wrap all this giant foreach contents' into a private method
2 things:
The first question is what prevented us from implementing this for subnamespaces previously, as we cannot accurately guess the namespace, nor enforce naming conventions on the classes themselves. The second question also quickly arose, due to the logic heuristics necessary to resolve. I think you may have these largely solved by having the Can you ask on the ML to see if you can get more people to test this, please? |
Using perfect real life example with Apigility:
For controller Performance impact should be neglectible since this mapping should happen once per dispatch. And i expect controller map to hold one entry per vendor or module name at most, in realistic usage scenario. Will post on ML a bit later |
👍 |
1 similar comment
👍 |
Put me down as 👍 as well. |
Add controller namespace prefix to template mapping Conflicts: tests/ZendTest/Mvc/View/InjectTemplateListenerTest.php
- Added docblock - Fluent interface
👍 Previous behaviour annoyed me too and did some impact. |
The PR containing documentation for this addition hasn't been merged (zendframework/zf2-documentation#1298) |
otherwise it would find the name in any place, including the controller name
Zf2 modules are not at the core of the framework unlike zf1.
We no longer rely on naming conventions but rather on design by contract. As such current controller to template name resolution, where only top level namespace and class name are used, makes no sense. And there are valid use cases where it does not work at all.
Consider controller class
Vendor\Module\Controller\FooController
:it will be resolved to
vendor/foo/action
. So asVendor\OtherModule\Controller\FooController
and evenVendor\OtherModule\Controller\Bar\FooController
.Unless... namespace parameter is specified in route, then resulting template name will suddenly change. Even if controller class does not match that namespace (surprise, mothaf...a!). Most unexpected and unreliable behaviour.
To fix that annoying bit this PR introduces new behaviour. for controller class to view template name resolution
It is very simple:
\Controller\
namespaceEg:
Xerkus\FooModule\Controller\Bar\FooController
->xerkus/foo-module/bar/foo/action
To prevent BC break, this behaviour will only be applied if namespace is whitelisted:
Most common use case is for modules that follow PSR-0, namely
<Vendor name>\(Namespace\)*<Class Name>
rule.As side effect of whitelist introduction, this PR introduces a way to specify a map of namespaces to template name prefixes
Eg
I consider that more of an edge case use case.
As side note: i'd want to see that behaviour as default in zf3, unless it will be handled completely differently.