モジュール階層ごとに特定のレイアウトを取得するためにZF2ソースを構築したい
次に、テンプレートファイルが存在する場合は、それを現在のモジュールにマップします
元:
- templates / album / profile / list.phtml:モジュール"アルバム"=>コントローラー"プロファイル"=>アクション"リスト"
- templates / album / album.phtml:すべての「アルバム」モジュール用
- templates / layout / layout.phtml:別のモジュールのデフォルトテンプレート
======================================
これはZF2スケルトンアプリで入手しました
'view_manager' => array (
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_map' => array (
'layout/layout' => __DIR__ . '/../../../template/layout/layout.phtml',
'layout/custom' => __DIR__ . '/../../../template/layout/custom.phtml',
'error/404' => __DIR__ . '/../../../template/error/404.phtml',
'error/index' => __DIR__ . '/../../../template/error/index.phtml'
),
'template_path_stack' => array (
__DIR__ . '/../view'
)
)
この方法でカスタムレイアウトを設定できます
$e->getApplication()->getEventManager()->getSharedManager()->attach('Zend\Mvc\Controller\AbstractActionController', 'dispatch', function($e) {
$controller = $e->getTarget();
$controller->layout('layout/custom');
}
ただし、「template_map」を手動で定義する必要があります。現在のコントローラー固有のテンプレートファイルを設定する方法はありますか?こんなもの欲しい
//Set template
$template = 'layout/layout';
if (file_exists($templatePath . $currAction.'.phtml')) {
$template = $currAction;
}else if(file_exists($templatePath . $currController.'.phtml')) {
$template = $currController;
}else if(file_exists($templatePath . $currModule.'.phtml')) {
$template = $currModule;
}else{
//return default template
}
$controller->layout($template);
ありがとう
================
2012年11月27日更新:
レイアウトキーをマップする簡単な方法を見つけました。
module.config.phpで、追加パスを追加するだけです
'view_manager' => array (
.....
'template_path_stack' => array (
__DIR__ . '/../view/',
__DIR__ . '/../../../template/'
)
)
次に、テンプレート階層システムを使用できます。ただし、モジュールおよびコントローラーレベルの場合のみ。私の問題は、レイアウトと「アクションビュー」の競合です
- /template/album.phtml=>正常に動作します。テンプレートをすべて「アルバム」モジュールに適用する
- /template/album/profile.phtml=>正常に動作します。テンプレートをすべて「アルバム/プロファイル」コントローラーに適用します
- /template/album/profile/detail.phtml=>「modules/album / view / album / profile/detail.phtml」と競合しています。次に、レイアウトファイルを表示するように設定すると、表示が間違ってしまいます