2014-08-27 50 views
0

在我的索引(/玩家)視圖中,我有一個帶有一堆玩家圖像的旋轉木馬。當我點擊圖像時,我會進入點擊播放器的視圖屏幕(/ players/view/1)。cakePHP:結合2個動作並加載所有玩家以及更改一個玩家

現在我想只有一個帶有該播放器圖像傳送帶的索引屏幕,並且當我點擊一個圖像後,我想要該播放器在同一索引屏幕上的信息。

我該如何最好地結合索引和視圖,以便我可以點擊播放器圖像並在同一頁上檢索他的信息?目標是在控制器中有一個視圖文件和一個動作。那麼我怎麼能把find('all')和find('first')放在一個動作中呢?

現在我有/球員和/球員/查看/ 1。 我想要例如/ players/1,因此它加載在同一頁面上。但是,這仍然會給我想一個頁面加載。

最終我不想要一個頁面加載,但只是一個內容的變化。 PlayersController的

索引操作(給我所有的球員):

public function index() { 
    $this->layout = 'default_front_players'; 
    $this->Player->recursive = 0; 
    //$this->Player->find('all'); 
    $this->set('players', $this->Paginator->paginate()); 
} 

視圖PlayersController的行動(讓我已經被點擊播放器):

public function view($id = null) { 
    $this->layout = 'default_front_players'; 

    if (!$this->Player->exists($id)) { 
     throw new NotFoundException(__('Invalid player')); 
    } 
    $options = array('conditions' => array('Player.' . $this->Player->primaryKey => $id)); 

    $this->set('player', $this->Player->find('first', $options)); 

} 

UPDATE

第一種情況

這給了我所有的玩家,並給我一個玩家(atm玩家143,數據庫中的最後一個玩家),問題是當我點擊玩家圖片時,玩家留在玩家143上。網址變成玩家/指數/ {點擊播放數}

public function index($id = null) { 
    $this->layout = 'default_front_players'; 
    $this->Player->recursive = 0; 

    $players = $this->Player->find('all'); 
    $this->set(compact('players')); 

    $options = array('conditions' => array('Player.' . $this->Player->primaryKey => $id)); 
    $player = $this->set('player', $this->Player->find('first', $options)); 
} 

第二種情況

這並不表明我的選手圖像,但是當我更改URL,它給我的球員的含量我在URL中給出。

public function index($id = null) { 
    $this->layout = 'default_front_players'; 
    $this->Player->recursive = 0; 

    //$players = $this->Player->find('all'); 
    //$this->Paginator->settings = $this->paginator_players; 
    $this->set('players'); 

    $options = array('conditions' => array('Player.' . $this->Player->primaryKey => $id)); 

    $player = $this->set('player', $this->Player->find('first', $options)); 
} 

如果我去/球員或球員/指數我沒有得到任何價值。

我該如何結合這兩個?

+0

「最佳方法」可能會帶來基於意見的答案。你可以再詳細一點嗎?有關最佳實踐的最佳方法?性能?更少的代碼寫作? – Nunser 2014-08-27 15:02:22

回答

0

您是否嘗試過使用ajax和路由來實現內容刷新(實質上創建您自己的api內部資源調用)?我使用cakephp作爲我自己的項目,並且需要一種方法來繞過整個控制器,查看加載機制以獲得更快的UI體驗。

要點是在您的模板或佈局文件中包含jquery。當某個路由或URL被調用時(這需要一個路由在app/config中的routes.php文件中相對於你的應用根文件夾設置),啓動ajax調用來獲取內容並動態更新dom,而不必重新加載頁面。

這個建議可能會爲你打開一個全新的蠕蟲罐,但它的可行性我認爲。

+0

謝謝,但首先我需要找到一種方法來組合索引和視圖。 – Gilko 2014-08-27 15:54:17