【问题标题】:cakephp 2 - load helper depending on layoutcakephp 2 - 根据布局加载助手
【发布时间】:2013-02-08 17:04:29
【问题描述】:

我想根据布局加载帮助程序。 我想要这样的东西,但 id 不起作用:

function beforeRender(){
  if (array_key_exists($this->request->action, $this->custom_layouts)){
    public $helpers = array('Html', 'Form', 'Session', 'Menu1');                
    $this->layout = $this->custom_layouts[$this->action];

  }else{
    public $helpers = array(
      'Session',
      'Html' => array('className' => 'TwitterBootstrap.BootstrapHtml'),
      'Form' => array('className' => 'TwitterBootstrap.BootstrapForm'),
      'Paginator' => array('className' => 'TwitterBootstrap.BootstrapPaginator'),
      'Menu1'
    );
    $this->layout = 'default';
  }
} 

感谢您的帮助 问候

【问题讨论】:

    标签: cakephp layout helpers cakephp-2.2


    【解决方案1】:

    你不能在方法中声明public $helpers

    你需要像 PHP 想要的那样调用它:

    $this->helpers[] = 'Menu1';
    $this->helpers['Html'] = array('className' => 'TwitterBootstrap.BootstrapHtml');
    ...
    

    【讨论】:

    • 请测试动态使用具有相同别名的不同帮助程序(例如,有时 $this->Html 映射到“BootstrapHtmlHelper”,有时映射到“HtmlHelper”)是否按预期工作。将相同的别名分配给不同的类可能会导致 Cake 缓存出现问题并可能导致意外结果(例如,随机附加“错误”类型的对象)。至少,“模型”就是这种情况,但同样的可能也适用于其他类
    • 非常感谢标记,完美运行。到目前为止,它按预期工作
    • @martin 很高兴听到,请务必将调试设置为 0 进行测试,因为只有这样缓存才能完全启用。
    • @thaJeztah 我现在尝试将调试设置为 0,它似乎可以工作。我将前端 twitter 引导插件用于具有调整布局的前端,然后用于管理后端,然后使用 cakephp 标准默认布局,而不使用 twitter 引导。我现在做什么:在 AppController 中,我不加载公共助手 html 和表单,因为我不使用自己的助手,所以这不是必需的,因为它们是由他们自己加载的。在检测到管理视图的情况下,o 加载 html、表单和分页器助手,如标记所示。
    猜你喜欢
    • 2011-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-03
    相关资源
    最近更新 更多