Skip to content

Instantly share code, notes, and snippets.

@TamiasSibiricus
Created July 8, 2014 14:31
Show Gist options
  • Save TamiasSibiricus/a1010fc95839f79e9e8a to your computer and use it in GitHub Desktop.
Save TamiasSibiricus/a1010fc95839f79e9e8a to your computer and use it in GitHub Desktop.
Possible prefixed layout solution
<?php
namespace App\View;
use Cake\View\View;
class CustomView extends View {
/**
* Returns layout filename for this template as a string.
*
* @param string $name The name of the layout to find.
* @return string Filename for layout file (.ctp).
* @throws \Cake\View\Error\MissingLayoutException when a layout cannot be located
*/
protected function _getLayoutFileName($name = null) {
if ($name === null) {
$name = $this->layout;
}
$subDir = null;
if ($this->layoutPath !== null) {
$subDir = $this->layoutPath . DS;
}
list($plugin, $name) = $this->pluginSplit($name);
$paths = $this->_paths($plugin);
//prefixed Layout lookup
if(isset($this->request->params['prefix']))
$files[] = ucfirst($this->request->params['prefix']).DS.'Layout' . DS . $subDir . $name;
//fallback for standard Layout or just use it if prefix is not set
$files[] = 'Layout' . DS . $subDir . $name;
foreach ($files as $file){
foreach ($paths as $path) {
if (file_exists($path . $file . $this->_ext)) {
return $path . $file . $this->_ext;
}
}
}
throw new Error\MissingLayoutException(array('file' => $file . $this->_ext));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment