ソースを参照

Merge pull request #3935 from ADmad/3.0-prefix-template-path

Allow having prefix specific layouts.
Mark Story 11 年 前
コミット
588dd6127e

+ 15 - 4
src/View/View.php

@@ -905,14 +905,25 @@ class View {
 		}
 		list($plugin, $name) = $this->pluginSplit($name);
 		$paths = $this->_paths($plugin);
-		$file = 'Layout' . DS . $subDir . $name;
+
+		$layoutPaths = ['Layout' . DS . $subDir];
+		if (!empty($this->request->params['prefix'])) {
+			array_unshift(
+				$layoutPaths,
+				Inflector::camelize($this->request->params['prefix']) . DS . $layoutPaths[0]
+			);
+		}
 
 		foreach ($paths as $path) {
-			if (file_exists($path . $file . $this->_ext)) {
-				return $path . $file . $this->_ext;
+			foreach ($layoutPaths as $layoutPath) {
+				if (file_exists($path . $layoutPath . $name . $this->_ext)) {
+					return $path . $layoutPath . $name . $this->_ext;
+				}
 			}
 		}
-		throw new Error\MissingLayoutException(array('file' => $file . $this->_ext));
+		throw new Error\MissingLayoutException(array(
+			'file' => $layoutPath[0] . $name . $this->_ext
+		));
 	}
 
 /**

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

@@ -569,6 +569,29 @@ class ViewTest extends TestCase {
 	}
 
 /**
+ * Test getting layout filenames for prefix
+ *
+ * @return void
+ */
+	public function testGetLayoutFileNamePrefix() {
+		$View = new TestView();
+
+		// Prefix specific layout
+		$View->request->params['prefix'] = 'FooPrefix';
+		$expected = TEST_APP . 'TestApp' . DS . 'Template' . DS .
+			'FooPrefix' . DS . 'Layout' . DS . 'default.ctp';
+		$result = $View->getLayoutFileName();
+		$this->assertPathEquals($expected, $result);
+
+		// Fallback to app's layout
+		$View->request->params['prefix'] = 'Admin';
+		$expected = TEST_APP . 'TestApp' . DS . 'Template' . DS .
+			'Layout' . DS . 'default.ctp';
+		$result = $View->getLayoutFileName();
+		$this->assertPathEquals($expected, $result);
+	}
+
+/**
  * Test for missing views
  *
  * @expectedException \Cake\View\Error\MissingViewException

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

@@ -0,0 +1 @@
+Default layout for FooPrefix prefix.