Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Enable view sub-directories when using ModuleRouteListener #2867

Closed
wants to merge 2 commits into from

3 participants

@mpinkston

This feature enables the InjectTemplateListener to specify view sub-directories when the ModuleRouteListener is being used.

For example: It allows a controller such as Foo\Controller\SubNamespace\BarController to map to "foo/sub-namespace/bar/(action)"

@mpinkston mpinkston Enable view sub-directories when using ModuleRouteListener
This feature enables the InjectTemplateListener to specify view sub-directories when the ModuleRouteListener is being used.

For example: It allows a controller such as Foo\Controller\SubNamespace\BarController to map to "foo/sub-namespace/bar/(action)"
ac74896
@weierophinney

Can you provide some unit tests, please?

@ghost Unknown referenced this pull request from a commit
@weierophinney weierophinney Merge branch 'hotfix/2867'
Close #2867
3c2aaeb
@ghost Unknown referenced this pull request from a commit
@weierophinney weierophinney Merge branch 'hotfix/2867' into develop
Forward port #2867
86a4554
@fabiocarneiro

@mpinkston argh, dont think removing the first 2 elements is a good idea. I'm working with submodules in the second element instead of in the 3rd like you're doing and it does not work for me even with proper setting in router __NAMESPACE__ index.

Module/Submodule/Controller

instead of

Module/Controller/Submodule.

@weierophinney you merged this :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 30, 2012
  1. @mpinkston

    Enable view sub-directories when using ModuleRouteListener

    mpinkston authored
    This feature enables the InjectTemplateListener to specify view sub-directories when the ModuleRouteListener is being used.
    
    For example: It allows a controller such as Foo\Controller\SubNamespace\BarController to map to "foo/sub-namespace/bar/(action)"
Commits on Oct 31, 2012
  1. @mpinkston
This page is out of date. Refresh to see the latest.
View
35 library/Zend/Mvc/View/Http/InjectTemplateListener.php
@@ -14,7 +14,7 @@
use Zend\EventManager\ListenerAggregateInterface;
use Zend\Filter\Word\CamelCaseToDash as CamelCaseToDashFilter;
use Zend\Mvc\MvcEvent;
-use Zend\Mvc\Router\RouteMatch;
+use Zend\Mvc\ModuleRouteListener;
use Zend\View\Model\ModelInterface as ViewModel;
class InjectTemplateListener implements ListenerAggregateInterface
@@ -90,9 +90,21 @@ public function injectTemplate(MvcEvent $e)
}
$module = $this->deriveModuleNamespace($controller);
- $controller = $this->deriveControllerClass($controller);
+ if ($namespace = $routeMatch->getParam(ModuleRouteListener::MODULE_NAMESPACE)) {
+ $controllerSubNs = $this->deriveControllerSubNamespace($namespace);
+ if (!empty($controllerSubNs)) {
+ if (!empty($module)) {
+ $module .= '/' . $controllerSubNs;
+ } else {
+ $module = $controllerSubNs;
+ }
+ }
+ }
+
+ $controller = $this->deriveControllerClass($controller);
$template = $this->inflectName($module);
+
if (!empty($template)) {
$template .= '/';
}
@@ -136,6 +148,25 @@ protected function deriveModuleNamespace($controller)
}
/**
+ * @param $namespace
+ * @return string
+ */
+ protected function deriveControllerSubNamespace($namespace)
+ {
+ if (!strstr($namespace, '\\')) {
+ return '';
+ }
+ $nsArray = explode('\\', $namespace);
+
+ // Remove the first two elements representing the module and controller directory.
+ $subNsArray = array_slice($nsArray, 2);
+ if (empty($subNsArray)) {
+ return '';
+ }
+ return implode('/', $subNsArray);
+ }
+
+ /**
* Determine the name of the controller
*
* Strip the namespace, and the suffix "Controller" if present.
View
29 tests/ZendTest/Mvc/View/InjectTemplateListenerTest.php
@@ -12,6 +12,7 @@
use PHPUnit_Framework_TestCase as TestCase;
use Zend\EventManager\EventManager;
+use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;
use Zend\Mvc\Router\RouteMatch;
use Zend\Mvc\View\Http\InjectTemplateListener;
@@ -102,6 +103,34 @@ public function testBypassesTemplateInjectionIfResultViewModelAlreadyHasATemplat
$this->assertEquals('custom', $model->getTemplate());
}
+ public function testMapsSubNamespaceToSubDirectoryWithControllerFromRouteMatch()
+ {
+ $this->routeMatch->setParam(ModuleRouteListener::MODULE_NAMESPACE, 'Aj\Controller\SweetAppleAcres\Reports');
+ $this->routeMatch->setParam('controller', 'CiderSales');
+ $this->routeMatch->setParam('action', 'PinkiePieRevenue');
+
+ $model = new ViewModel();
+ $this->event->setResult($model);
+ $this->listener->injectTemplate($this->event);
+
+ $this->assertEquals('sweet-apple-acres/reports/cider-sales/pinkie-pie-revenue', $model->getTemplate());
+ }
+
+ public function testMapsSubNamespaceToSubDirectoryWithControllerFromEventTarget()
+ {
+ $this->routeMatch->setParam(ModuleRouteListener::MODULE_NAMESPACE, 'ZendTest\Mvc\Controller\TestAsset');
+ $this->routeMatch->setParam('action', 'test');
+
+ $myViewModel = new ViewModel();
+ $myController = new \ZendTest\Mvc\Controller\TestAsset\SampleController();
+
+ $this->event->setTarget($myController);
+ $this->event->setResult($myViewModel);
+ $this->listener->injectTemplate($this->event);
+
+ $this->assertEquals('zend-test/controller/test-asset/sample/test', $myViewModel->getTemplate());
+ }
+
public function testAttachesListenerAtExpectedPriority()
{
$events = new EventManager();
Something went wrong with that request. Please try again.