laminas / laminas-view

Flexible view layer supporting and providing multiple view layers, helpers, and more
https://docs.laminas.dev/laminas-view/
BSD 3-Clause "New" or "Revised" License
74 stars 46 forks source link

Url View-helper: better exception wording on router->assemble() fail #2

Open weierophinney opened 4 years ago

weierophinney commented 4 years ago

This PR will change 2 things:

  1. Add a comment to the HelperPluginManager that the "url" and "basePath" View-Helper factories are overwritten in the ViewHelperManagerFactory of zend-mvc.

  2. Change the exception text of the "url" View-helper if the router cant assemble the URL to include more information:

Before:

Zend\Router\Exception\InvalidArgumentException: Missing parameter "my-parameter" in /var/www/vendor/zendframework/zend-router/src/Http/Segment.php:309
Stack trace:
#0 /var/www/vendor/zendframework/zend-router/src/Http/Segment.php(419): Zend\Router\Http\Segment->buildPath(Array, Array, false, false, Array)
#1 /var/www/vendor/zendframework/zend-router/src/Http/TreeRouteStack.php(370): Zend\Router\Http\Segment->assemble(Array, Array)
#2 /var/www/vendor/zendframework/zend-router/src/Http/Part.php(216): Zend\Router\Http\TreeRouteStack->assemble(Array, Array)
#3 /var/www/vendor/zendframework/zend-router/src/Http/TreeRouteStack.php(370): Zend\Router\Http\Part->assemble(Array, Array)
#4 /var/www/vendor/zendframework/zend-router/src/Http/Part.php(216): Zend\Router\Http\TreeRouteStack->assemble(Array, Array)
#5 /var/www/vendor/zendframework/zend-router/src/Http/TreeRouteStack.php(370): Zend\Router\Http\Part->assemble(Array, Array)
#6 /var/www/vendor/zendframework/zend-router/src/Http/Part.php(216): Zend\Router\Http\TreeRouteStack->assemble(Array, Array)
#7 /var/www/vendor/zendframework/zend-router/src/Http/TreeRouteStack.php(391): Zend\Router\Http\Part->assemble(Array, Array)
#8 /var/www/vendor/zendframework/zend-mvc-i18n/src/Router/TranslatorAwareTreeRouteStack.php(111): Zend\Router\Http\TreeRouteStack->assemble(Array, Array)
#9 /var/www/vendor/zendframework/zend-view/src/Helper/Url.php(106): Zend\Mvc\I18n\Router\TranslatorAwareTreeRouteStack->assemble(Array, Array)
#10 [internal function]: Zend\View\Helper\Url->__invoke('my/route/...', Array, Array, false)
#11 /var/www/vendor/zendframework/zend-view/src/Renderer/PhpRenderer.php(397): call_user_func_array(Object(Zend\View\Helper\Url), Array)
#12 /var/www/module/xxx/view/xxx/yyy.phtml(101): Zend\View\Renderer\PhpRenderer->__call('url', Array)
...

After:

Zend\View\Helper\Exception\InvalidArgumentException: Couldnt create URL for route "my/route/do-it", params "{}" and options "[]" in /var/www/vendor/zendframework/zend-view/src/Helper/Url.php:109
Stack trace:
#0 [internal function]: Zend\View\Helper\Url->__invoke('my/route/...', Array)
#1 /var/www/vendor/zendframework/zend-view/src/Renderer/PhpRenderer.php(397): call_user_func_array(Object(Zend\View\Helper\Url), Array)
#2 /var/www/module/xxx/view/xxx/yyy.phtml(101): Zend\View\Renderer\PhpRenderer->__call('url', Array)
...

Advantages:

Open questions:


Originally posted by @MatthiasKuehneEllerhold at https://github.com/zendframework/zend-view/pull/200

weierophinney commented 4 years ago

@MatthiasKuehneEllerhold Thanks for your contribution. We need unit tests to cover these changes. Thanks !


Originally posted by @michalbundyra at https://github.com/zendframework/zend-view/pull/200#issuecomment-553784614

weierophinney commented 4 years ago

The zend-router only throws the Zend\Mvc\Router exceptions if its 2.x. Thats only tested in the DEPS=lowest setting on travis, see here: https://travis-ci.org/zendframework/zend-view/jobs/611765820?utm_medium=notification&utm_source=github_status

Cant seem to replicate it locally though. But i dont have 7.1 set up anymore.

So the Zend\Mvc\Router exceptions will be tested at DEPS=lowest while the Zend\Router will be tested at DEPS=latest.


Originally posted by @MatthiasKuehneEllerhold at https://github.com/zendframework/zend-view/pull/200#issuecomment-553843440