2016-01-20 47 views
0

我在CakePHP中构建了一个应用程序(我在一个新工作中使用了这个工作了几个月,并从前一个员工接管了这个工程,所以我的理解是有限的)。如何在一个视图中引用多个控制器(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> 

我得到的错误是:

Error: Call to a member function find() on null File: /Applications/XAMPP/xamppfiles/htdocs/licensemanagementsite/app/Controller/CentresController.php Line: 87

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

任何援助,你可以给我会非常感激。我的办公室里没有人知道CakePHP的任何信息(包括我,我想!)。

谢谢!

+0

CentresController.php 87行中有什么 –

+0

在视图内部使用requestAction方法。或者在控制器中,如果你想在那个地方。 $ this-> requestAction('controller/action); array('named'=> array('limit'=> 3) echo $ this-> requestAction( array('controller'=>'articles','action'=>'featured')) ) ; array $('pass'=> array(5)) – Sudhir

+0

line 87:> $(0)$ this-> requestAction( array('controller'=>'articles','action'=>'view'), array categories = $ this-> Software-> Category-> find('list'); – dplatt

回答

0

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

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

0

有几种实现你想要的方法。 请记住,一个模型与一个表相关联,首先应该注意的是,一个控制器不需要仅与一个模型关联。对于使用公共变量“使用”,在这种情况下,您的中心控制器,我们将是具有这样的:

<?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版,在yours-搜索)从中心控制器 然后你只需拨打$this->Software->fetchSoftwareType($filters['category'])即可获得您的软件,具体取决于类别。
类似地,您可以从相同的控制器调用相似的数据,调用中心模型中的相应方法,例如$this->Centre->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变量设定的数据,过程,并设置它来显示它的视图。

相关问题