【问题标题】:CakePHP set variable in view for use in layoutCakePHP 在视图中设置变量以用于布局
【发布时间】:2013-04-21 07:25:06
【问题描述】:

我有一个 CRUD 应用程序,在我看来,有来自各种控制器的操作的链接,例如

<?php echo $this->Html->link(__('List Docs'), array('controller' => 'docs', 'action' => 'index')); ?>
<?php echo $this->Html->link(__('Add Doc'), array('controller' => 'docs', 'action' => 'add')); ?>
<?php echo $this->Html->link(__('List Images'), array('controller' => 'images', 'action' => 'index')); ?>
<?php echo $this->Html->link(__('Add Image'), array('controller' => 'images', 'action' => 'add')); ?>

//etc..

现在,我还有一个带有侧边栏的 default.ctp 布局,我想用正在呈现的每个视图的操作链接动态填充它。我知道我可以将操作从我的控制器移动到它们各自的模型并在控制器内的 beforeRender() 回调中设置变量,但是我想将我的操作保留在控制器中,而是在视图中设置一个数组和将其传递给 default.ctp 布局。到目前为止,这是我所拥有的:

Docs/index.ctp

$links_array = array(
    'list_docs' => array('controller' => 'docs', 'action' => 'index'),
    'add_doc' => array('controller' => 'docs', 'action' => 'add'),
    'list_images' => array('controller' => 'images', 'action' => 'index'),
    'add_image' => array('controller' => 'images', 'action' => 'add')
    );
$this->set('links', $links_array);

Layouts/default.ctp

print_r($links);

这会返回Notice (8): Undefined variable: links [APP\View\Layouts\default.ctp, line 93] 我猜是因为布局是在视图之前呈现的。

在不将动作转移到模型中的情况下,最好的方法是什么?

【问题讨论】:

    标签: php cakephp layout cakephp-2.1


    【解决方案1】:
    $links_array = array(
    'list_docs' => array('controller' => 'docs', 'action' => 'index'),
    'add_doc' => array('controller' => 'docs', 'action' => 'add'),
    'list_images' => array('controller' => 'images', 'action' => 'index'),
    'add_image' => array('controller' => 'images', 'action' => 'add')
    );
    $this->set('links', $links_array);
    

    ... 应该在控制器中。

    布局将看到视图中可用的任何变量。所以$links 将在布局中可见。 (如果你真的需要从视图而不是控制器设置变量,你不需要在视图中使用$this-&gt;set(),只需使用$links = ...)。

    【讨论】:

      【解决方案2】:

      您是否考虑过使用视图块?该手册甚至使用侧边栏作为其使用示例; Using View Blocks

      // In a view file.
      // Create a navbar block
      $this->startIfEmpty('navbar');
      echo $this->element('navbar', array('links' => $links_array));
      $this->end();
      
      // In a parent view/layout
      echo $this->fetch('navbar');
      

      【讨论】:

      • 非常漂亮,我倾向于在控制器中定义一个数组,保持代码分开,但我一定要记住这一点。
      • 嗯,这两种方法都是实现它的有效方法,这就是我添加这个答案的原因 - 向(未来)读者展示替代方案
      【解决方案3】:

      这更好:using blocks for script and css files

      您可以定义块名称,例如 scriptBottom
      将内容附加到它并显示在布局或其他视图的正确位置。

      // In your view
      $this->Html->script('carousel', ['block' => 'scriptBottom']);
      $this->Html->script('custom', ['block' => 'scriptBottom']);
      
      //or
      $this->startIfEmpty('scriptBottom');
      $this->append('scriptBottom', $this->script('custom2'));
      
      // In your layout or another view
      <?= $this->fetch('scriptBottom') ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-26
        • 1970-01-01
        • 1970-01-01
        • 2014-01-07
        • 1970-01-01
        相关资源
        最近更新 更多