Consultez la FAQ sur le ZF avant de poster une question
Vous n'êtes pas identifié.
Pages: 1
Bonjour,
J'expérimente actuellement le framework Zend dans sa version 2.
Je reste déconcerté par la difficulté de mettre en place le modèle MVC aussi simple soit-il...
Je travaille actuellement sous environnement WAMP (donc sous Windows) et j'essaie de me faire la main avec ce framework en m'appuyant sur un petit site tout simple :
- 1 header,
- 1 footer
=> là je suis pas mal
- dans le "header" un menu avec trois "items" => "Accueil", "Prestations" et "Contact".
L'item "Accueil" pointe vers mon "IndexController" (celui par défaut si j'ai bien compris) et mon contenu s'affiche bien entre mon "Header" et mon "Footerr". L'URL affichée dans mon navigateur est alors "http://localhost/monsite/"
Par contre j'éprouve les plus grandes difficultés pour afficher le contenu du mon "container" "Prestations".
Lorsque j'appelle l'URL "http://localhost/monsite/prestations/" j'ai le message d'erreur suivant : "404 - Not Found - The requested URL could not be matched by routing."...
Dans mon arborescence j'ai :
Module\Application\src\Application\Controller\PrestationsController.php :
<?php namespace Application\Controller; use Zend\Mvc\Controller\AbstractActionController; use Zend\View\Model\ViewModel; class PrestationsController extends AbstractActionController { public function indexAction() { return new ViewModel(); } }
Module\Application\view\application\prestations\index.pthml :
définissant le contenu HTML a afficher tout simplement.
Je pense avoir compris que c'était dans le fichier de configuration "Module\Application\config\module.config.php" qu'il manquait quelquechose... J'ai essayé plusieurs choses suite à mes recherches sur le NET, toutes différentes les unes des autres (ce qui me réconforte pas). J'ai essayé d'ajouter une route "Prestations" à différents niveaux de ce fichier de configuration mais rien n'y fait... et j'ai actuellement :
<?php /** * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ return array( 'router' => array( 'routes' => array( 'home' => array( 'type' => 'Zend\Mvc\Router\Http\Literal', 'options' => array( 'route' => '/', 'defaults' => array( 'controller' => 'Application\Controller\IndexController', 'action' => 'index', ), ), ), // The following is a route to simplify getting started creating // new controllers and actions without needing to create a new // module. Simply drop new controllers in, and you can access them // using the path /application/:controller/:action 'application' => array( 'type' => 'Literal', 'options' => array( 'route' => '/', 'defaults' => array( '__NAMESPACE__' => 'Application\Controller', 'controller' => 'Index', 'action' => 'index', ), ), 'may_terminate' => true, 'child_routes' => array( 'default' => array( 'type' => 'Segment', 'options' => array( 'route' => '/[:controller[/:action]]', 'constraints' => array( 'controller' => '[a-zA-Z][a-zA-Z0-9_-]*', 'action' => '[a-zA-Z][a-zA-Z0-9_-]*', ), 'defaults' => array( ), ), ), ), ), ), ), 'service_manager' => array( 'factories' => array( 'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory', ), ), 'translator' => array( 'locale' => 'en_US', 'translation_file_patterns' => array( array( 'type' => 'gettext', 'base_dir' => __DIR__ . '/../language', 'pattern' => '%s.mo', ), ), ), 'controllers' => array( 'invokables' => array( 'Application\Controller\Index' => 'Application\Controller\IndexController', [b][color=#FF0000]'Application\Controller\Prestations' => 'Application\Controller\PrestationsController'[/color][/b] ), ), 'view_manager' => array( 'display_not_found_reason' => true, 'display_exceptions' => true, 'doctype' => 'HTML5', 'not_found_template' => 'error/404', 'exception_template' => 'error/index', 'template_map' => array( 'layout/layout' => __DIR__ . '/../view/layout/layout.phtml', 'header' => __DIR__ . '/../view/layout/header.phtml', 'footer' => __DIR__ . '/../view/layout/footer.phtml', 'application/index/index' => __DIR__ . '/../view/application/index/index.phtml', 'error/404' => __DIR__ . '/../view/error/404.phtml', 'error/index' => __DIR__ . '/../view/error/index.phtml', ), 'template_path_stack' => array( __DIR__ . '/../view', 'prestations' => __DIR__ . '/../view' ), ), );
Enfin voici ma configuration Apache ou j'ai bien mis le AllowOverride All :
NameVirtualHost monsite:80 <VirtualHost LocalHost> ServerName monsite DocumentRoot "C:/......./Zend/workspaces/DefaultWorkspace/monsite/public" # This should be omitted in the production environment SetEnv APPLICATION_ENV development <Directory "C:/...../Zend/workspaces/DefaultWorkspace/monsite/public"> Options Indexes MultiViews FollowSymLinks [b]AllowOverride All[/b] Order allow,deny Allow from all </Directory> </VirtualHost>
Voilà j'en suis donc venu à me diriger vers vous afin d'obtenir des réponses à ce problème.
Je crois que je n'ai pas bien compris le principe de ce fichier de configuration et sur lequel je n'ai trouvé aucune documentation claire.
En vous remerciant par avance pour vos réponses.
Dernière modification par cid007300 (07-02-2013 11:22:32)
Hors ligne
Bonjour,
Le soucis vient du routage comme indiqué dans le message d'erreur.
1° Tu a 2 fois la route "/" déclarée
2° Le routage ne sait pas quoi faire de "/prestations" il faut que les allias soient correctement déclarés pour que ça fonctionne
<?php /** * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ return array( 'controllers' => array( 'invokables' => array( // Allias pour les controllers 'index' => 'Application\Controller\IndexController', 'prestations' => 'Application\Controller\PrestationsController' ), ), 'router' => array( 'routes' => array( // Route par defaut 'index' => array( 'type' => 'Literal', 'options' => array( 'route' => '/', 'defaults' => array( 'controller' => 'Index', 'action' => 'index', ), ), 'may_terminate' => true, 'child_routes' => array( 'default' => array( 'type' => 'Segment', 'options' => array( 'route' => '/[:controller[/:action]]', 'constraints' => array( 'controller' => '[a-zA-Z][a-zA-Z0-9_-]*', 'action' => '[a-zA-Z][a-zA-Z0-9_-]*', ), 'defaults' => array( 'controller' => 'index', // Allias du controller 'action' => 'index', // Action par defaut ), ), ), ), ), // Une route qui ne porte pas le nom de son controller... // 'prestations' => array( // 'type' => 'Literal', // 'options' => array( // 'route' => '/toto[/:action]', // 'constraints' => array( // 'action' => '[a-zA-Z][a-zA-Z0-9_-]*', // Optionel... // ), // 'defaults' => array( // 'controller' => 'prestations', // Allias du controller // 'action' => 'index', // Action par defaut // ), // ), // ), ), ), );
Hors ligne
Ce qui est mal expliqué dans la doc ou les exemples c'est que la table de Routage cherche un allias qui correspond a l'URL
http://monsite.com/allias/action/.....
il faut donc que le nom "allias" soit dans la table controllers invokables.
'allias' => 'Application\Controller\TotoController'
Hors ligne
jfvole a écrit:
Bonjour,
Le soucis vient du routage comme indiqué dans le message d'erreur.
1° Tu a 2 fois la route "/" déclarée
2° Le routage ne sait pas quoi faire de "/prestations" il faut que les allias soient correctement déclarés pour que ça fonctionneCode:
<?php /** * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ return array( 'controllers' => array( 'invokables' => array( // Allias pour les controllers 'index' => 'Application\Controller\IndexController', 'prestations' => 'Application\Controller\PrestationsController' ), ), 'router' => array( 'routes' => array( // Route par defaut 'index' => array( 'type' => 'Literal', 'options' => array( 'route' => '/', 'defaults' => array( 'controller' => 'Index', 'action' => 'index', ), ), 'may_terminate' => true, 'child_routes' => array( 'default' => array( 'type' => 'Segment', 'options' => array( 'route' => '/[:controller[/:action]]', 'constraints' => array( 'controller' => '[a-zA-Z][a-zA-Z0-9_-]*', 'action' => '[a-zA-Z][a-zA-Z0-9_-]*', ), 'defaults' => array( 'controller' => 'index', // Allias du controller 'action' => 'index', // Action par defaut ), ), ), ), ), // Une route qui ne porte pas le nom de son controller... // 'prestations' => array( // 'type' => 'Literal', // 'options' => array( // 'route' => '/toto[/:action]', // 'constraints' => array( // 'action' => '[a-zA-Z][a-zA-Z0-9_-]*', // Optionel... // ), // 'defaults' => array( // 'controller' => 'prestations', // Allias du controller // 'action' => 'index', // Action par defaut // ), // ), // ), ), ), );
Merci beaucoup pour cette première réponse. La déclaration des "alias" et de leur utilisation dans la définition des "routes" me semblent plus clair maintenant.
Malheureusement après avoir effectué les modifications dans le fichier module.config.php mes deux routes ne fonctionne plus...
1. localhost/monsite/ me retourne Application\Controller\IndexController\Index(resolves to invalid controller class or alias: Application\Controller\IndexController\Index)
2. localhost/monsite/prestations me retourne The requested URL could not be matched by routing.
Voici mon module.config.php modifié :
<?php /** * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ return array( 'controllers' => array( 'invokables' => array( 'index' => 'Application\Controller\IndexController', 'prestations' => 'Application\Controller\PrestationsController' ), ), 'router' => array( 'routes' => array( 'index' => array( 'type' => 'Literal', 'options' => array( 'route' => '/', 'defaults' => array( '__NAMESPACE__' => 'Application\Controller\IndexController', 'controller' => 'Index', 'action' => 'index', ), ), 'may_terminate' => true, 'child_routes' => array( 'default' => array( 'type' => 'Segment', 'options' => array( 'route' => '/[:controller[/:action]]', 'constraints' => array( 'controller' => 'index', 'action' => 'index', ), 'defaults' => array( ), ), ), ), ), 'prestations' => array( 'type' => 'Literal', 'options' => array( 'route' => '/prestations[/:action]', 'constraints' => array( 'action' => '[a-zA-Z][a-zA-Z0-9_-]*', // Optionel... ), 'defaults' => array( 'controller' => 'prestations', // Allias du controller 'action' => 'index', // Action par defaut ), ), ), ), ), 'service_manager' => array( 'factories' => array( 'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory', ), ), 'translator' => array( 'locale' => 'en_US', 'translation_file_patterns' => array( array( 'type' => 'gettext', 'base_dir' => __DIR__ . '/../language', 'pattern' => '%s.mo', ), ), ), 'view_manager' => array( 'display_not_found_reason' => true, 'display_exceptions' => true, 'doctype' => 'HTML5', 'not_found_template' => 'error/404', 'exception_template' => 'error/index', 'template_map' => array( 'layout/layout' => __DIR__ . '/../view/layout/layout.phtml', 'header' => __DIR__ . '/../view/layout/header.phtml', 'footer' => __DIR__ . '/../view/layout/footer.phtml', 'application/index/index' => __DIR__ . '/../view/application/index/index.phtml', 'error/404' => __DIR__ . '/../view/error/404.phtml', 'error/index' => __DIR__ . '/../view/error/index.phtml', ), 'template_path_stack' => array( __DIR__ . '/../view', 'prestations' => __DIR__ . '/../view' ), ), );
Par avance merci pour votre aide.
Hors ligne
Il reste une majuscule a Index.... qui ne devrait pas y etre
Hors ligne
jfvole a écrit:
Il reste une majuscule a Index.... qui ne devrait pas y etre
Merci pour votre réponse.
J'ai enfin réussi a obtenir quelque-chose de fonctionnel, mais avec quelques ajustements venant en plus de vos informations, que je n'explique pas clairement je l'avoue, à savoir :
1. j'ai du mettre en commentaire l'attribut '__NAMESPACE__' => 'Application\Controller\IndexController' de la route index->options->defaults sinon le message d'erreur était Application\Controller\IndexController\Index(resolves to invalid controller class or alias: Application\Controller\IndexController\Index).
Comme-ci ZF recherchait un alias s'appelant ou lié à Application\Controller\IndexController\Index. Ce qui n'est pas le cas. Du coup à quoi sert cet attribut ?
2. j'ai du enlever le [/:action] de la route prestations->options->route et les contraintes associées. En lisant les premiers tutoriaux de ZF2 j'avais bien souligné le rôle de la définition des différentes actions au sein d'un même controlleur. A priori soit une action doit toujours être définit avec une URL valide type /controlleur/action soit aucune. Ce dernier cas ressemble au mien puisque je n'ai qu'une action pour mon controlleur prestations. Est-bien cela ? Peut-être que le noeud child_routes est-il destiné à cela ?
Je trouve la documentation sur ZF2 terriblement pauvre si on enlève le tutoriel présenté partout sur la gestion des Albums. Niveau bouquin de référence cela ne semble pas mieux. Peut-être suis-je passé à côté de ressources sur le web ?
En vous remerciant pour votre temps et vos réponses.
Ci joint mon fichier module.config.php actuel après modification :
<?php /** * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ return array( 'controllers' => array( 'invokables' => array( 'index' => 'Application\Controller\IndexController', 'prestations' => 'Application\Controller\PrestationsController' ), ), 'router' => array( 'routes' => array( 'index' => array( 'type' => 'Literal', 'options' => array( 'route' => '/', 'defaults' => array( /*'__NAMESPACE__' => 'Application\Controller\IndexController',*/ 'controller' => 'index', 'action' => 'index', ), ), 'may_terminate' => true, 'child_routes' => array( 'default' => array( 'type' => 'Segment', 'options' => array( 'route' => '/[:controller[/:action]]', 'constraints' => array( 'controller' => 'index', 'action' => 'index', ), 'defaults' => array( ), ), ), ), ), 'prestations' => array( 'type' => 'Literal', 'options' => array( 'route' => '/prestations', //[/:action] /* 'constraints' => array( 'action' => '[a-zA-Z][a-zA-Z0-9_-]*', // Optionel... ),*/ 'defaults' => array( 'controller' => 'prestations', // Allias du controller 'action' => 'index', // Action par defaut ), ), ), ), ), 'service_manager' => array( 'factories' => array( 'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory', ), ), 'translator' => array( 'locale' => 'en_US', 'translation_file_patterns' => array( array( 'type' => 'gettext', 'base_dir' => __DIR__ . '/../language', 'pattern' => '%s.mo', ), ), ), 'view_manager' => array( 'display_not_found_reason' => true, 'display_exceptions' => true, 'doctype' => 'HTML5', 'not_found_template' => 'error/404', 'exception_template' => 'error/index', 'template_map' => array( 'layout/layout' => __DIR__ . '/../view/layout/layout.phtml', 'header' => __DIR__ . '/../view/layout/header.phtml', 'footer' => __DIR__ . '/../view/layout/footer.phtml', 'application/index/index' => __DIR__ . '/../view/application/index/index.phtml', 'application/prestations/index' => __DIR__ . '/../view/application/prestations/index.phtml', 'error/404' => __DIR__ . '/../view/error/404.phtml', 'error/index' => __DIR__ . '/../view/error/index.phtml', ), 'template_path_stack' => array( __DIR__ . '/../view', 'prestations' => __DIR__ . '/../view' ), ), );
Hors ligne
Pour ma part je ne met jamais le namespace. Et si je devais le mettre se serait :
Application\Controller;
Les actions entre [/:actions] sont de toutes façons optionnelles. De même il est possible de spécifier dans la route des paramètres du genre [/:actions[/:param1/:param2]] que l'on peut récupérer dans le controller.
Hors ligne
Merci beaucoup à vous pour toutes ces informations précieuses.
Le fichier "module.config.php" est maintenant plus clair pour moi, en ce qui concerne les routes et les controlleurs.
Hors ligne
Pages: 1