2012-06-16 1 views
0

Je commence à développer en utilisant Zend Framework et j'ai une question sur les routes. Est-il un moyen de au lieu d'avoir une URL comme ceci:Zend Route - Supprimer le nom du paramètre de l'URL

www.mysite.com/newsletter/groups/edit/id/1

avoir ceci:

www.mysite.com/newsletter/groupes/modifier/1

(sans le nom du paramètre id)

J'ai déjà mis ce code pour déclarer un itinéraire personnalisé dans mon fichier bootStrap:

protected function _initRoutes() 
{ 
    $router = Zend_Controller_Front::getInstance()->getRouter();  

    /* Edit Groups */ 
    $route = new Zend_Controller_Router_Route('groups/edit/:group_id',array('controller' => 'groups','action' => 'edit')); 
    $router->addRoute('group_edit', $route); 

    return $router; 
} 

Puis dans mon dossier de vue, je l'utiliser pour faire écho l'URL:

<a href="<?=$this->url(array('group_id' => $group->getId()), 'group_edit');?>" class=""><?=$group->getName()?></a> 

Et l'URL est faisant écho à la façon dont je veux:

<a href="/fuhrmann/newsletter/groups/edit/1" class="">Group 1</a> 

Ceci est mon application.ini:

[production] 

appnamespace = "Application" 

; Debug output 
phpSettings.display_startup_errors = 0 
phpSettings.display_errors = 0 
resources.frontController.params.displayExceptions = 0 

;PHP Setings 
phpsettings.date.timezone = "America/Sao_Paulo" 

; Include path 
includePaths.models = APPLICATION_PATH "/models" 
includePaths.application = APPLICATION_PATH 
includePaths.library = APPLICATION_PATH "/../library" 

; Bootstrap 
bootstrap.path = APPLICATION_PATH "/Bootstrap.php" 
bootstrap.class = "Bootstrap" 


; Front Controller 
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers" 
resources.frontController.env = APPLICATION_ENV 
resources.frontController.actionHelperPaths.Action_Helper = APPLICATION_PATH "/views/helpers" 
resources.frontController.moduleControllerDirectoryName = "actions" 
;resources.frontController.moduleDirectory = APPLICATION_PATH "/modules" 
resources.frontController.defaultControllerName = "index" 
resources.frontController.defaultAction = "index" 
resources.frontController.defaultModule = "default" 
;resources.frontController.baseUrl = "/newsletter" 
;resources.frontController.returnresponse = 1 

; Layout 
resources.layout.layout = "layout" 
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/" 


; Views 
resources.view.helperPath = APPLICATION_PATH "/views/helpers" 
resources.view.encoding = "UTF-8" 
resources.view.basePath = APPLICATION_PATH "/views" 
resources.view.scriptPath.Default = APPLICATION_PATH "/views/scripts" 
resources.view.doctype = "HTML5" 
resources.view.contentType = "text/html;charset=utf-8" 
resources.view.helperPathPrefix = "Views_Helpers_" 
resources.view.filterPathPrefix = "Views_Filters_" 

resources.db.adapter = "PDO_SQLITE" 


[testing : production] 
phpSettings.display_startup_errors = 1 
phpSettings.display_errors = 1 
resources.frontController.params.displayExceptions = 1 


[development : production] 
phpSettings.display_startup_errors = 1 
phpSettings.display_errors = 1 
resources.frontController.params.displayExceptions = 1 
resources.frontController.throwExceptions = true 

resources.db.adapter = "PDO_MYSQL" 
resources.db.params.host = "localhost" 
resources.db.params.dbname = "newsletter" 
resources.db.params.username = "root" 
resources.db.params.password = "" 
resources.db.isDefaultTableAdapter = true 
resources.db.params.charset = utf8 

Mon dossier complet de Boostrap:

Mon index.php (à l'intérieur du public) Fichier:

<?php 
// Define path to application directory 
defined('APPLICATION_PATH') 
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application')); 

// Define application environment 
defined('APPLICATION_ENV') 
    || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production')); 

// Ensure library/ is on include_path 
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'), 
    get_include_path(), 
))); 

/** Zend_Application */ 
require_once 'Zend/Application.php'; 

// Create application, bootstrap, and run 
$application = new Zend_Application(
    APPLICATION_ENV, 
    APPLICATION_PATH . '/configs/application.ini' 
); 
$application->bootstrap() 
      ->run(); 

Le problème est, quand je clique pour ouvrir cette page (la page du groupe d'édition) je reçois cette erreur:

Fatal error: Uncaught exception 'Zend_Controller_Router_Exception' with message 'group_id is not specified' in C:\xampp\htdocs\fuhrmann\newsletter\library\Zend\Controller\Router\Route.php:354 Stack trace: #0 C:\xampp\htdocs\fuhrmann\newsletter\library\Zend\Controller\Router\Rewrite.php(470): Zend_Controller_Router_Route->assemble(Array, true, true) #1 C:\xampp\htdocs\fuhrmann\newsletter\library\Zend\View\Helper\Url.php(49): Zend_Controller_Router_Rewrite->assemble(Array, NULL, true, true) #2 [internal function]: Zend_View_Helper_Url->url(Array, NULL, true) #3 C:\xampp\htdocs\fuhrmann\newsletter\library\Zend\View\Abstract.php(350): call_user_func_array(Array, Array) #4 C:\xampp\htdocs\fuhrmann\newsletter\application\layouts\scripts\layout.phtml(22): Zend_View_Abstract->__call('url', Array) #5 C:\xampp\htdocs\fuhrmann\newsletter\application\layouts\scripts\layout.phtml(22): Zend_View->url(Array, NULL, true) #6 C:\xampp\htdocs\fuhrmann\newsletter\library\Zend\View.php(108): include('C:\xampp\htdocs...') #7 C:\xampp\htdocs\fu in C:\xampp\htdocs\fuhrmann\newsletter\library\Zend\Controller\Plugin\Broker.php on line 336 

Dans mon action EDIT, je peux faire un var_dump dans tous les params de requête pour voir si le groupId est défini, et oui, c'est le cas!

array(3) { ["groupId"]=> string(3) "555" ["controller"]=> string(6) "groups" ["action"]=> string(6) "edit" } 

Je l'ai déjà cherché beaucoup de réponses ici et d'ailleurs, I found a question une réponse, mais pas de solution pour moi.

Merci!

+2

Veuillez publier vos solutions en tant que réponse, et non en tant que modification de la question. –

Répondre

3

Ok, résolu!

Ce que je l'ai fait était d'ajouter une valeur par défaut (NULL) au group_id, donc dans mon dossier Bootstrap je maintenant:

$route = new Zend_Controller_Router_Route('/groups/edit/:group_id',array('controller' => 'groups','action' => 'edit', 'group_id' => NULL)); 

Merci @philien!

0

Vous pouvez en supprimant le trait de soulignement:

protected function _initRoutes() 
{ 
    $router = Zend_Controller_Front::getInstance()->getRouter();  

    /* Edit Groups */ 
    $route = new Zend_Controller_Router_Route('groups/edit/:groupId',array('controller' => 'groups','action' => 'edit')); 
    $router->addRoute('group_edit', $route); 

    return $router; 
} 

Et après:

<a href="<?=$this->url(array('groupId' => $group->getId()), 'group_edit');?>" class=""><?=$group->getName()?></a> 

sont mauvais à Souligné utiliser dans ce cas, je choisis de mettre des noms dans CamelCased.

+0

J'ai essayé de changer ce que vous avez dit, mais je reçois toujours l'erreur. Peut-être quelque chose dans mon fichier boostrap ... J'ai édité ma réponse avec le code complet du fichier bootstrap. Si je pointe vers ** http: // localhost/newsletter/groupes/edit/groupId/1 ** tout fonctionne. – Fuhrmann

Questions connexes