在我的索引(/玩家)視圖中,我有一個帶有一堆玩家圖像的旋轉木馬。當我點擊圖像時,我會進入點擊播放器的視圖屏幕(/ 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));
}
如果我去/球員或球員/指數我沒有得到任何價值。
我該如何結合這兩個?
「最佳方法」可能會帶來基於意見的答案。你可以再詳細一點嗎?有關最佳實踐的最佳方法?性能?更少的代碼寫作? – Nunser 2014-08-27 15:02:22