Zend Route - 从URL中删除参数名称

时间:2012-06-16 15:31:36

标签: zend-framework zend-route

我开始使用Zend Framework进行开发,我对路由有疑问。有没有办法代替这样的URL:

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

有这个:

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

(没有参数名称 id

我已经把这段代码用来在我的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;
}

然后在我的视图文件中,我使用它来回显URL:

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

网址回应我想要的方式:

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

这是我的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

我的完整Boostrap文件:

<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected function _initDoctype()
    {
        $this->bootstrap('view');
        $view = $this->getResource('view');
        $view->doctype('XHTML1_STRICT');
    }

    /**
     * Init Autoloader
     */
    protected function _initAutoload()
    {
        $loader = Zend_Loader_Autoloader::getInstance();
        $loader->setFallbackAutoloader(true);
    }

    /**
     * Adiciona alguns routers
     */
    protected function _initRoutes()
    {

        $router = Zend_Controller_Front::getInstance()->getRouter();


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


        return $router;
    }

}

我的index.php(公共内部)文件:

<?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();

问题是,当我点击打开此页面(编辑组页面)时,我收到此错误:

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

在我的编辑操作中,我可以在所有请求参数中执行var_dump,看看是否设置了groupId,是的,它是!

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

我已经在这里寻找了很多答案,顺便说一下,I found a question给出了答案,但没有解决方案。

谢谢!

2 个答案:

答案 0 :(得分:3)

好的,已解决

我所做的是向group_id添加一个默认值(NULL),所以在我的Bootstrap文件中我现在有了这个:

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

谢谢@philien!

答案 1 :(得分:0)

您可以尝试删除下划线:

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;
}

之后:

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

在这种情况下使用Underscore是不好的,我选择将名字放在CamelCased中。