Consultez la FAQ sur le ZF avant de poster une question
Vous n'êtes pas identifié.
Bonjour !
Je débute avec le framework Zend et je suis le tuto de Rob Allen traduit par Guillaume Rossolini, que la plupart doit connaître...
J'ai commencé avec la navigation, et je souhaite aussi rajouter une page style répertoire de contacts.
J'ai donc dupliqué les scripts relatifs aux albums du tuto, version contacts.
Et j'ai mis en place un menu, qui s'affiche bien.
Mais les liens ne semblent pas correspondre aux pages, et j'ai des erreurs qui s'affichent.
Je pense qu'il y a une ou plusieurs étapes que je n'ai pas dû comprendre correctement.
Je me permet donc de poster les erreurs qui se présentent. Si une bonne âme pouvait non pas forcément les corriger mais m'indiquer une piste, que je puisse comprendre ce que je fais.
Bien sûr j'ai cherché des solutions parmi toutes les ressources mais je ne vois toujours pas.
Tout d'abord le xml de navigation
<configdata> <nav> <home> <label>home</label> <controller>index</controller> <pages> <album> <label>Albums</label> <controller>Album</controller> </album> <contact> <label>Contacts</label> <controller>contact</controller> </contact> </pages> </home> </nav> </configdata>
Puis les erreurs qui s'affichent, ici pour le lien Contact
An error occurred Page not found Exception information: Message: Action "index" does not exist and was not trapped in __call() Stack trace: #0 G:\wamp\www\site-zf4\library\Zend\Controller\Action.php(515): Zend_Controller_Action->__call('indexAction', Array) #1 G:\wamp\www\site-zf4\library\Zend\Controller\Dispatcher\Standard.php(295): Zend_Controller_Action->dispatch('indexAction') #2 G:\wamp\www\site-zf4\library\Zend\Controller\Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http)) #3 G:\wamp\www\site-zf4\library\Zend\Application\Bootstrap\Bootstrap.php(97): Zend_Controller_Front->dispatch() #4 G:\wamp\www\site-zf4\library\Zend\Application.php(366): Zend_Application_Bootstrap_Bootstrap->run() #5 G:\wamp\www\site-zf4\public\index.php(26): Zend_Application->run() #6 {main} Request Parameters: array ( 'controller' => 'contact', 'action' => 'index', 'module' => 'default', )
Pour le lien albums
Page not found Exception information: Message: Invalid controller class ("AlbumController")
Mon fichier ContactController.php
<?php class ContactController extends Zend_Controller_Action { public function init() { /* Initialize action controller here */ $activeNav = $this->view->navigation()->findByController('contact'); $activeNav->active = true; //$activeNav->setClass("active"); } public function indexContact() { $contacts = new Application_Model_DbTable_Contacts(); $this->view->contacts = $contacts->fetchAll(); } public function addContact() { $form = new Application_Form_Contact(); $form->submit->setLabel('Enregistrer'); $this->view->form = $form; if ($this->getRequest()->isPost()) { $formData = $this->getRequest()->getPost(); if ($form->isValid($formData)) { $genre = $form->getValue('genre'); $nom = $form->getValue('nom'); $prenom = $form->getValue('prenom'); $adresse1 = $form->getValue('adresse1'); $adresse2 = $form->getValue('adresse2'); $codepostal = $form->getValue('codepostal'); $ville = $form->getValue('ville'); $tel = $form->getValue('tel'); $portable = $form->getValue('portable'); $email = $form->getValue('email'); $observation = $form->getValue('observation'); $contacts = new Application_Model_DbTable_Contacts(); $contacts->addContact($genre, $nom, $prenom, $adresse1, $adresse2, $codepostal, $ville, $tel, $portable, $email, $observation); $this->_helper->redirector('contact'); } else { $form->populate($formData); } } } public function editContact() { $form = new Application_Form_Contact(); $form->submit->setLabel('Enregistrer'); $this->view->form = $form; if ($this->getRequest()->isPost()) { $formData = $this->getRequest()->getPost(); if ($form->isValid($formData)) { $id = (int)$form->getValue('id'); $genre = $form->getValue('genre'); $nom = $form->getValue('nom'); $prenom = $form->getValue('prenom'); $adresse1 = $form->getValue('adresse1'); $adresse2 = $form->getValue('adresse2'); $codepostal = $form->getValue('codepostal'); $ville = $form->getValue('ville'); $tel = $form->getValue('tel'); $portable = $form->getValue('portable'); $email = $form->getValue('email'); $observation = $form->getValue('observation'); $contacts = new Application_Model_DbTable_Contacts(); $contacts->updateContact($id, $genre, $nom, $prenom, $adresse1, $adresse2, $codepostal, $ville, $tel, $portable, $email, $observation); $this->_helper->redirector('contact'); } else { $form->populate($formData); } } else { $id = $this->_getParam('id', 0); if ($id > 0) { $contacts = new Application_Model_DbTable_Contacts(); $form->populate($contacts->getContact($id)); } } } public function deleteContact() { if ($this->getRequest()->isPost()) { $del = $this->getRequest()->getPost('Supprimer'); if ($del == 'Oui') { $id = $this->getRequest()->getPost('id'); $contacts = new Application_Model_DbTable_Contacts(); $contacts->deleteContact($id); } $this->_helper->redirector('contact'); } else { $id = $this->_getParam('id', 0); $contacts = new Application_Model_DbTable_Contacts(); $this->view->contact = $contacts->getContact($id); } } }
Dans models/DbTable/Contacts.php
<?php class Application_Model_DbTable_Contacts extends Zend_Db_Table_Abstract { protected $_name = 'contacts'; public function getContact($id) { $id = (int)$id; $row = $this->fetchRow('id = ' . $id); if (!$row) { throw new Exception("Could not find row $id"); } return $row->toArray(); } public function addContact($genre, $nom, $prenom, $adresse1, $adresse2, $codepostal, $ville, $tel, $portable, $email, $observation) { $data = array( 'genre' => $genre, 'nom' => $nom, 'prenom' => $prenom, 'adresse1' => $adresse1, 'adresse2' => $adresse2, 'codepostal' => $codepostal, 'ville' => $ville, 'tel' => $tel, 'portable' => $portable, 'email' => $email, 'observation' => $observation, ); $this->insert($data); } public function updateContact($id, $genre, $nom, $prenom, $adresse1, $adresse2, $codepostal, $ville, $tel, $portable, $email, $observation) { $data = array( 'genre' => $genre, 'nom' => $nom, 'prenom' => $prenom, 'adresse1' => $adresse1, 'adresse2' => $adresse2, 'codepostal' => $codepostal, 'ville' => $ville, 'tel' => $tel, 'portable' => $portable, 'email' => $email, 'observation' => $observation, ); $this->update($data, 'id = '. (int)$id); } public function deleteContact($id) { $this->delete('id =' . (int)$id); } }
Voilà pour le code, j'espère que c'est point trop abusé de tout posté comme ça.
Je crois qu'il y a un truc qui m'échappe.
Merci d'avance pour vos pistes !
Je continue à chercher de mon côté.
Nicus.
Hors ligne
Bonjour,
j'ai trouvé la source de mon problème: les noms des actions dans les fonctions de mon controller.
Zend demande de la rigueur ! donc acte !
Nicus.
Hors ligne
Bonjour Nicus,
Je complète un peu ton auto-réponse.
C' est exact, les méthodes action doivent s'appeler xxxxAction où xxxx est le nom de l'action.
Pour ton action add, elle doit donc s'appeler addAction et non pas addContact (avec add en minuscules).
Je le précise pour d'autres qui auraient le même problème de compréhension de la logique ZEND.
Hors ligne