Zend FR

Consultez la FAQ sur le ZF avant de poster une question

Vous n'êtes pas identifié.

#1 19-10-2010 23:22:02

Nicus
Nouveau membre
Date d'inscription: 19-10-2010
Messages: 2

probleme de navigation et de Controllers

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

Code:

 <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

Code:

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

Code:

Page not found
Exception information:

Message: Invalid controller class ("AlbumController")

Mon fichier ContactController.php

Code:

<?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

Code:

<?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. neutral
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

 

#2 21-10-2010 11:10:27

Nicus
Nouveau membre
Date d'inscription: 19-10-2010
Messages: 2

Re: probleme de navigation et de Controllers

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 ! smile

Nicus.

Hors ligne

 

#3 22-10-2010 09:58:14

kami66
Membre
Date d'inscription: 18-10-2010
Messages: 12

Re: probleme de navigation et de Controllers

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

 

Pied de page des forums

Propulsé par PunBB
© Copyright 2002–2005 Rickard Andersson
Traduction par punbb.fr

Graphisme réalisé par l'agence Rodolphe Eveilleau
Développement par Kitpages