【问题标题】:How to reference more than one controller in one view (CakePHP)如何在一个视图中引用多个控制器(CakePHP)
【发布时间】:2016-01-20 13:41:06
【问题描述】:

我正在使用 CakePHP 构建一个应用程序(我在一份新工作中使用了几个月,并从以前的员工那里接手了这个项目,所以我的理解有限)。

这个应用程序有几个数据库表,涉及到这个问题的是中心、软件和类别。

简要介绍一下,我需要的是,当用户访问“中心”视图时,能够获得下面所有软件的列表,并勾选这些中心可以访问哪些软件。

要查看软件列表,我的 SoftwaresController 使用以下功能:

public function view() {

$categories = $this->Software->Category->find ('list');
array_unshift ($categories, array('any' => 'Any'));
$this->set ('categories', $categories);
$this->Software->hasMany['Version']['limit'] = 1;
$this->Software->hasMany['Version']['fields'] = array('Version.id', 'Version.version_number', 'Version.created');
if ($this->request->is ('post') && $this->request->data) {
  $conditions = array();
  $filters = $this->request->data['Software'];
  if (!empty($filters['name'])) {
    $conditions['Software.name'] = $filters['name'];
  }
  if ($filters['category'] > 0) {
    $conditions['Category.id'] = $filters['category'];
  }
  $this->paginate = array(
      'conditions' => $conditions,
      'order' => array(
          'Software.latest_released' => 'desc',
          'Software.name' => 'asc'
      )
  );
  $this->set ('softwares', $this->paginate ());
  $this->set ('paging', false);
} else {
  $this->paginate = array(
      'order' => array(
          'Software.latest_released' => 'desc',
          'Software.name' => 'asc'
      )
  );
  $this->set ('paging', true);
  $this->set ('softwares', $this->paginate ());
}
// This allows the user software page to show.
}

在 CentresController 函数中,我还希望能够在其中查看数据(如上面提取的),如下所示:

public function admin_view($id = null)
{

    /* Centres */
    $this->Centre->recursive = 0;
    $this->Centre->id = $id;
    if (!$this->Centre->exists()) {
        throw new NotFoundException(__('This centre ID does not exist.'));
    }
    $centre = $this->Centre->read(null, $id);
    $this->request->data = $centre;
    $this->set('centre', $centre);
}

我尝试通过将 view() 的内部添加到 admin_view() 中来组合上述两个函数。 admin_view() 的视图如下所示:

分配软件访问权限

                      <div class="row-fluid">
                        <div class="span12">
                          <div class="table-responsive">
                            <table class="table table-striped table-bordered table-hover">
                              <thead>
                              <tr>
                                <th>Version Number</th>
                                <th>Created</th>
                                <th>Modified</th>
                                <th>Released</th>
                                <th>Grant Permission</th>
                              </tr>
                              </thead>
                              <tbody>
                              <?php
                              $i = 0;
                              foreach ($software['Version'] as $version):
                                $class = null;
                                if ($i++ % 2 == 0) {
                                  $class = ' class="even"';
                                } else {
                                  $class = ' class="odd"';
                                }
                                ?>
                                <tr<?php echo $class;?>>
                                  <td><?php echo $version['version_number']; ?>&nbsp;</td>
                                  <td>
                                    <?php
                                    echo $this->Time->format (Configure::read ('Format.time'), $version['created']);
                                    ?>&nbsp;
                                  </td>
                                  <td>
                                    <?php
                                    echo $this->Time->format (Configure::read ('Format.time'), $version['modified']);
                                    ?>&nbsp;
                                  </td>
                                  <td>
                                    <?php
                                    echo $this->Time->format (Configure::read ('Format.time'), $version['released']);
                                    ?>&nbsp;
                                  </td>
                                  <td class="actions" style="text-align: center;">
                                    <?php
                                    echo $this->Html->link (
                                        '<i class="fa fa-cloud-download"></i>&nbsp;Download',
                                        array(
                                            'controller' => 'versions',
                                            'action' => 'view',
                                            $version['id']
                                        ),
                                        array('escape' => false)
                                    );
                                    ?>&nbsp;

                                    </a>
                                  </td>
                                </tr>
                              <?php endforeach; ?>
                              </tbody>
                            </table>
                          </div>
                          <!-- /.table-responsive -->







                    </div>
                </fieldset>
                <div class="col-sm-12 col-md-5 col-lg-4 padding-right-0">
                    <div class="panel panel-default">
                        <div class="panel-heading"><span class="fa fa-cog"></span>&nbsp;Actions</div>
                        <div class="panel-body">
                            <div class="row">
                                <div class="col-sm-4 padding-bottom-10">
                                    <?php
                                    echo $this->Form->button(
                                        '<i class="fa fa-pencil"></i>&nbsp;Update',
                                        array(
                                            'type' => 'submit',
                                            'class' => 'btn btn-success btn-sm btn-block',
                                            'escape' => false
                                        )
                                    );
                                    echo $this->Form->end();
                                    ?>
                                </div>
                                <!-- /.col-sm-4 padding-bottom-10 -->
                                <div class="col-sm-4 padding-bottom-10">
                                    <a class="btn btn-danger btn-sm btn-block" href="#modal-dialog"
                                       data-toggle="modal"
                                       data-target="#modal-dialog"><i class="fa fa-trash"></i>&nbsp;Delete</a>
                                </div>
                                <!-- /.col-sm-4 padding-bottom-10 -->
                                <div class="col-sm-4 padding-bottom-10">
                                    <?php
                                    echo $this->Html->link(
                                        "<span class='fa fa-times'></span>&nbsp;Cancel",
                                        array(
                                            'action' => 'index'
                                        ),
                                        array(
                                            'escape' => false,
                                            'class' => 'btn btn-default btn-sm btn-block'
                                        )
                                    );
                                    ?>
                                </div>
                                <!-- /.col-sm-4 padding-bottom-10 -->
                            </div>
                            <!-- /.row -->
                        </div>

我得到的错误是:

错误:在 null 上调用成员函数 find() 文件:/Applications/XAMPP/xamppfiles/htdocs/licensemanagementsite/app/Controller/CentresController.php 线路:87

我还在 CentresController 和 Centers 模型的顶部包括: App::uses('AppController', 'Controller', 'SoftwaresController');

您能给我的任何帮助将不胜感激。我办公室里没有人对 CakePHP 有任何了解(包括我,我猜!)。

谢谢!

【问题讨论】:

  • CentresController.php 的第 87 行有什么
  • 在视图中使用 requestAction 方法。如果你想在那个地方,或者在控制器中。 $this->requestAction('控制器/动作); echo $this->requestAction(array('controller' => 'articles', 'action' => 'featured'), array('named' => array('limit' => 3)) ); echo $this->requestAction(array('controller' => 'articles', 'action' => 'view'), array('pass' => array(5))
  • 第 87 行:> $categories = $this->Software->Category->find ('list');

标签: php cakephp controller models


【解决方案1】:

在 CakePhp 中,视图文件只能从其关联控制器获取输出。您不能在视图文件中调用控制器。两个控制器也不能一起使用,即如果您想将 A 控制器的功能用于 B 控制器,则不能直接使用,或者可能不是一个好习惯。

如果您想在两个控制器之间使用一些通用功能,那么您应该使用“组件”。在此处阅读Components 以创建组件。 将函数创建为组件后,您可以在任何控制器中使用该函数,只需将组件包含在其中即可。使用组件,您可以将组件的结果设置为变量,然后该变量可以在关联视图文件中使用。

【讨论】:

    【解决方案2】:

    有几种方法可以实现您想要的。 请记住,一个模型与一个表相关联,首先您应该知道的是,一个控制器不需要仅与一个模型相关联。为此,在这种情况下,您在 Centers 控制器中使用公共变量“uses”,我们将拥有如下内容:

    <?php
    class CentresController extends AppController
    {
      public uses = array(Centre, Software);
    
      public function action1()
      {...}
      ...
    }
    

    然后您应该将获取数据的逻辑移动到您的模型中,并从控制器中获取数据。假设您想要特定类别的软件。然后在软件模型中,您将创建如下函数:

    public function fetchSoftwareType($type)
    {
      return $this->find('all', array('conditions'=>array(category=>$type)));
    }
    

    (有关此内容的更多信息,请查看Models/Retrieving Your Data -此链接适用于版本 2.X,请在您的中搜索-) 然后从您的 Centers 控制器中,您只需调用$this-&gt;Software-&gt;fetchSoftwareType($filters['category']),即可根据类别获取您的软件。
    类似地,您可以检索从同一控制器调用到 Center 模型中相应方法的类似数据,例如 $this-&gt;Centre-&gt;fetchCentreData($whatever)

    <?php
    class CentresController extends AppController
    {
      public uses = array(Centre, Software);
    
      public function view()
      {
        $softData = $this->Software->fetchSoftwareType($filters['category']);
        $centreData = $this->Centre->fetchCentreData($whatever);
        //.... process the data
        //$resultForView = $this->processData($softData, $centreData);
        //....
        $this->set('centre', $resultForView);
      }
      ...
    }
    

    关键是,您可以从 uses 变量中设置的任意多个模型中获取所需的数据,对其进行处理并将其设置为视图以显示它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-07
      • 2017-11-12
      相关资源
      最近更新 更多