php 在 cakephp 中分配布局
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7426469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Assigning Layout in cakephp
提问by techie_28
Can we define a Layout for whole controller in that particular controller?I have used before filter of the appcontroller for this purpose earlier but it doesnt solves it any more.So i need that there should be some definition of layout in controller that would apply for all the actions of that controller.
我们可以在那个特定的控制器中为整个控制器定义一个布局吗?我之前为此目的使用过 appcontroller 的过滤器之前,但它不再解决它了。所以我需要在控制器中应该有一些布局的定义可以申请该控制器的所有操作。
Regards
问候
回答by sukinsan
use it:
用它:
inside your action
在你的行动中
$this->layout = 'mylayout';
you have to create that layout in view/layout/mylayout.ctp
您必须在 view/layout/mylayout.ctp 中创建该布局
or add this function to controller to set layout for each action of controller
或将此功能添加到控制器以设置控制器的每个动作的布局
function beforeFilter() {
parent::beforeFilter();
$this->layout = 'mylayout';
}
回答by Vins
The best way to do some thing like this
做这样的事情的最好方法
var $layout = 'my_account';
This will apply to your entire controller. After adding this code it will look something like this.
这将适用于您的整个控制器。添加此代码后,它将看起来像这样。
class MyAccountsController extends AppController {
var $name = 'MyAccounts';
var $components = array('Email');
var $layout = 'my_account';
If you do not want to use some of the action you can explicitly define in your action like this
如果您不想使用某些操作,您可以像这样在操作中明确定义
function my_action(){
$this->layout = 'another_layout';
}
Now this my_action
will take another layout and rest of the action will take my_account
layout
现在这my_action
将采用另一个布局,其余的动作将采用my_account
布局
回答by TommyDo
回答by vins
Yes we can. You just need to create your layout file under Template>>Layout>>yourlayout.ctp Then load this layout along with the Controller by:
我们可以。你只需要在 Template>>Layout>>yourlayout.ctp 下创建你的布局文件然后通过以下方式加载这个布局和控制器:
class MyController extends AppController {
public function initialize(){
parent :: initialize();
$this->layout = "yourlayout";
}
}
this will automatically implement this layout as default for the Controller. you might want to use $this->viewBuilder->layout("yourlayout"); inside the initialize() but it leads to error "Call to member function layout() on boolean". So instead you can use the former.
这将自动实现此布局作为控制器的默认布局。你可能想使用 $this->viewBuilder->layout("yourlayout"); 在 initialize() 内部,但会导致错误“调用成员函数 layout() on boolean”。因此,您可以使用前者。