2016-01-20 48 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變量設定的數據,過程,並設置它來顯示它的視圖。

相關問題