Zend Framework虚荣网页分页不起作用

时间:2013-03-14 15:32:16

标签: php zend-framework zend-route zend-paginator vanity-url

我正在开发一个Zend Framework应用程序,我只是使用http://tfountain.co.uk/blog/2010/9/9/vanity-urls-zend-framework/中的指南来完成我的虚拟路由,但我的分页不起作用,并且没有任何虚荣网址指南我能够找到触摸它。

我的虚荣路线设置为/: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; ?>

$this->url()不返回'/ jaimerump / badges / 3',而是返回'/'。我确信这与我的自定义路由类有关。我从Tim Fountain的教程改变了匹配功能,我将类的名称更改为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。它调用什么功能,我是否需要重载它?

1 个答案:

答案 0 :(得分:0)

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

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

由于$data['username']未定义,我正在回复'/';路由器只提供页面参数。编写自己的自定义路由类时,汇编函数必须为未在数据数组中传入的任何段提供默认值。修正了:

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