Sadece PRG Pattern .. Çok basit doğru do?! Eh, en azından herkes öyle diyor ama kimse net bir cevap nakleder! Bana arama ve kazma bir hafta sürdü ve daha sonra "Acemi" kendi başına bir şeyler yapmaya karar verdim! İşte (I 2.0.5 kullanıyorum) CakePHP'de bunu yapmak için bir yoludur:
Regardless of code here is the logic in steps:
1- set data
2- validate (do NOT create() yet)
3- write $this->request->data to a session variable
4- redirect to a saveData action
Inside saveData action:
5- read & save the session's variable
6- DELETE session's variable
7- create()
8- save data to model
9- redirect
Here is an example of how your code might look like.
**Attn: "ourController" and "ourModel"
public function add() {
if ($this->request->is('post')) {
if (isset($this->request->data)) {
$this->ourModel->set($this->request->data);
if ($this->ourModel->validates()) {
$this->Session->write('myData', $this->request->data);
$this->redirect(array('controller' => 'ourController',
'action' => 'saveData',
'ourModel' //optional but recommended
)
);
} else {
$this->Session->setFlash('ourModel could not be saved.');
}
}
.....//the rest of add() function
}
Sonra index eylemine tekrar sizi yönlendirir veya mantık sizi nereye götürürse bu işlev için (doğrulama üzerine) yönlendirilmesi gerektiğini!
public function saveData($model) {
$myData = $this->Session->read('myData');
$this->Session->delete('myData'); //extremely important
$this->$model->create();
if ($this->$model->save($myData))
// or $myData[$model] if you are dealing with multiple models
{
$this->Session->setFlash(__($model.' have been saved successfully'));
$this->redirect(array('controller' => 'ourController',
'action' => 'index'
)
);
}
} else{
$this->Session->setFlash(__($model.' could not be saved'));
}
}
}
Basit bir öz-yönlendirme işe yarayabilir ama çoğu durumda (başka bir form veya dizin görünümüne örneğin) farklı bir görünüme yönlendirmek istiyorum
Ben sadece böyle işlevselliği sunucu tarafı yapmak için (benim durumumda olduğu gibi) bütün bir hafta harcamak zorunda değil bu yüzden bu detaylandırılması başkalarına zamandan tasarruf yardımcı olur umarım!