如何使用cakephp在另一个视图中调用视图


How to call view in another view by using cakephp

我只是新的CakePhp,我想知道如何在另一个视图中调用视图。

当我开始运行CakePhp时,默认布局位于view/layouts/default.ctp

在<<p> strong>违约。ctp我调用了一个视图名homeview (view/homes/homeview.ctp)。 下面是我的代码:
<?php 
     echo $this->fetch('homeview'); // this statement here is work
?>

homeview中。ctp我调用了另一个名为displayphone的视图(view/homes/displayphone.ctp) homeview。ctp

<?php $this->start('homeview'); ?>
    <h1> This is home view </h1>
    <?php echo $this->fetch('displayphone'); // this statement does not work; ?>
<?php $this->end(); ?>

displayphone.ctp

<?php $this->start('displayphone');?>
    <h1> This page display phone </h1>
<?php $this->end(); ?>

为什么不能在homeview中调用displayphone block ?

正如你所说,

 $this->fetch('homeview');

创建了一个名为homeview的块,参考http://book.cakephp.org/2.0/en/views.html

在视图中调用另一个视图是不可能的,除非你为它创建一个元素。元素是通用的HTML集合,可以在整个项目中的任何视图文件中使用。为了上述目的,创建一个名为"displayphone.ctp"的元素。在元素文件夹中,然后命名为
 $this->element('displayphone');

是的,你可以在另一个视图中调用一个视图(不是很好,但你可以这样做)。

例如,你有2个视图视图/用户/login.ctp视图/用户/register.ctp

而你想在login view中调用register view

// File login.ctp
$this->Element('../Users/register.ctp'); // ('..' . DS . 'Users' . DS . 'register.ctp')

为此,您需要创建元素

对于CakePHP 3,使用

<?= $this->requestAction('/Users/register') ?>