1

我正在开发一个 Zend Framework 应用程序,我刚刚使用http://tfountain.co.uk/blog/2010/9/9/vanity-urls-zend-framework/上的指南让我的虚荣路由工作,但我的分页不起作用,我找不到任何虚荣的 url 指南。

我的虚荣路线设置为/:username/:filter/:pag',其中最后两个参数是可选的。页面显示正确,“/jaimerump/badges/2”将转到我的徽章的第二页,但分页器无法正常工作,因为它似乎无法确定下一页的 url 应该是什么。我正在使用每个 Zend 分页教程中的标准 paginationControl.phtml 文件,代码如下:

<?php if ($this->pageCount): ?>
<div class="paginationControl" id="paginationControl">
<!-- Previous page link -->
<?php if (isset($this->previous)): ?>
    <a href="<?php echo $this->url(array('page' => $this->previous)); ?>" id="previous">
        &lt; Previous
    </a> |
<?php else: ?>
    <span class="disabled">&lt; Previous</span> |
<?php endif; ?>

<!-- Infinite Scroll doesn't use numbered page links -->

<!-- Next page link -->
<?php if (isset($this->next)): ?>
    <a id="next" href="<?php echo $this->url(array('page' => $this->next)); ?>" >
    Next &gt;
    </a>
<?php else: ?>
    <span class="disabled">Next &gt;</span>
<?php endif; ?>
</div>
<?php endif; ?>

不是返回“/jaimerump/badges/3”,而是$this->url()返回“/”。我确信这与我的自定义路由类有关。我从 Tim Fountain 的教程中更改了 match 函数,并将类的名称更改为 VanityRouteUser 并将其放在我的模型文件夹中,但除此之外,一切都是相同的。匹配功能:

public function match( $path, $partial = false )
{
    /* User profile routes are of form /:username/:filter/:page
    Where filter can be haves, wants, badges, etc.
    */

    if ($path instanceof Zend_Controller_Request_Http) {
        $path = $path->getPathInfo();
    }

    $path = trim($path, $this->_urlDelimiter);
    $pathBits = explode($this->_urlDelimiter, $path);

    if ( count($pathBits) < 1 ) {
        return false;
    }

    // check database for this user
    $result = DB::call()->fetchRow('SELECT user_id 
                                    FROM users 
                                    WHERE username = ?', $pathBits[0]);
    if ($result) {
        // user found
        $values = $this->_defaults;
        $values['username'] = $pathBits[0];
        $values['page'] = ( empty( $pathBits[2] ) )?1:$pathBits[2]; //if they didn't provide a page

        // figure out which action based on second segment
        $filter = $pathBits[1];
        if( empty( $filter ) || $filter == 'default' 
            || $filter == 'haves' || $filter == 'wants' ){
            //Looking for user's haves, wants, or both 
            $values['action'] = 'index';
            $values['filter'] = ( empty($filter) )?'default':$filter; //To catch the blank
        }
        else if( $filter == 'badges' ){
            //Looking for user's badges
            $values['action'] = 'badges';
        }
        else if( $filter == 'shelves' ){
            //Looking for user's shelves
            $values['action'] = 'shelves';
        }
        else{
            //Must be a shelf id
            $values['action'] = 'index';
            $values['filter'] = $filter;
        }

        return $values;
    }

    return false;
} 

是什么$this->url()?我无法在 Zend Paginator 中找到 url 函数。我假设它正在调用路由类中的某个函数来尝试获取下一页的 url。它调用了什么函数,我必须重载它吗?

4

1 回答 1

0

问题是汇编功能。我试图使用 Tim Fountain 教程中提供的功能,即:

public function assemble($data = array(), $reset = false, $encode = false)
{
    return $data['username'];
}

我得到了'/',因为$data['username']它是未定义的;路由器只提供了一个页面参数。在编写您自己的自定义路由类时,您的 assemble 函数必须为未在数据数组中传入的任何段提供默认值。修复它:

public function assemble($data = array(), $reset = false, $encode = false)
{
    //Check if username was provided
    $username = ( $data['username'] )?$data['username']:$this->_defaults['username'];

    //Check if filter was provided
    $filter = ( $data['filter'] )?$data['filter']:$this->_defaults['filter'];

    //Check if page was provided
    $page = ( $data['page'] )?$data['page']:$this->_defaults['page'];

    return $username.'/'.$filter.'/'.$page;
}
于 2013-03-14T15:50:01.137 回答