Browse Source

The "autoswitch elements to prefix path" functionality now cascades up if the controller runs in a nested prefixed namespace

Optimization
Yves P 11 years ago
parent
commit
42cbefea1b

+ 12 - 9
src/View/View.php

@@ -1005,7 +1005,8 @@ class View
     /**
      * Find all sub templates path, based on $basePath
      * If a prefix is defined in the current request, this method will prepend
-     * the prefixed template path to the $basePath.
+     * the prefixed template path to the $basePath, cascading up in case the prefix
+     * is nested.
      * This is essentially used to find prefixed template paths for elements
      * and layouts.
      *
@@ -1016,14 +1017,16 @@ class View
     {
         $paths = [$basePath];
         if (!empty($this->request->params['prefix'])) {
-            $prefixPath = array_map(
-                'Cake\Utility\Inflector::camelize',
-                explode('/', $this->request->params['prefix'])
-            );
-            array_unshift(
-                $paths,
-                implode('/', $prefixPath) . DS . $basePath
-            );
+            $prefixPath = explode('/', $this->request->params['prefix']);
+            $path = '';
+            foreach ($prefixPath as $prefixPart) {
+                $path .= Inflector::camelize($prefixPart) . DS;
+
+                array_unshift(
+                    $paths,
+                    $path . $basePath
+                );
+            }
         }
 
         return $paths;

+ 10 - 0
tests/TestCase/View/ViewTest.php

@@ -681,6 +681,12 @@ class ViewTest extends TestCase
         $result = $View->getLayoutFileName();
         $this->assertPathEquals($expected, $result);
 
+        $View->request->params['prefix'] = 'foo_prefix/bar_prefix';
+        $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS .
+            'FooPrefix' . DS . 'Layout' . DS . 'nested_prefix_cascade.ctp';
+        $result = $View->getLayoutFileName('nested_prefix_cascade');
+        $this->assertPathEquals($expected, $result);
+
         // Fallback to app's layout
         $View->request->params['prefix'] = 'Admin';
         $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS .
@@ -837,6 +843,10 @@ class ViewTest extends TestCase
         $this->View->request->params['prefix'] = 'FooPrefix/BarPrefix';
         $result = $this->View->element('prefix_element');
         $this->assertEquals('this is a nested prefixed test element', $result);
+
+        $this->View->request->params['prefix'] = 'FooPrefix/BarPrefix';
+        $result = $this->View->element('prefix_element_in_parent');
+        $this->assertEquals('this is a nested prefixed test element in first level element', $result);
     }
 
     /**

+ 1 - 0
tests/test_app/TestApp/Template/FooPrefix/Element/prefix_element_in_parent.ctp

@@ -0,0 +1 @@
+this is a nested prefixed test element in first level element

+ 0 - 0
tests/test_app/TestApp/Template/FooPrefix/Layout/nested_prefix_cascade.ctp