Search code examples
routeszend-framework2zend-framework3

add userid parameter to route configuration in ZF pagination


Here's my code so far:

<?php 
echo $this->paginationControl($this->paginator,'sliding','partial/paginator.phtml',array('route' => 'adminuserlog'));
?>

My error is:

Missing parameter "userid"

How do I go about appending the userid param to the route portion of the call? The value of the user id is $this->user->getId()


Solution

  • The last parameter of the PaginationControl viewHelper is the way to pass data to your pagination partial/view.

    echo $this->paginationControl(
        $this->paginator,
        'sliding',
        'partial/paginator.phtml',
        array(
            'route' => 'adminuserlog',
            'routeParams' => array('userId' => $this->user->getId())
        ),
    );
    

    As the partial/paginator.phtml is just a view we can access any view variable by either using $route or $this->route.

    Update your partial/paginator.phtml file to include the $routeParams:

    $routeParams = isset($this->routeParams) && is_array($this->routeParams) ? $this->routeParams : [];
    $queryParams = isset($this->queryParams) && is_array($this->queryParams) ? $this->queryParams : [];
    
    $urlOptions = ['query' => $queryParams];
    
    // Rendering a button and usage of the URL viewhelper
    <a href="<?= $this->url(
        $route,
        $routeParams,
        ArrayUtils::merge($urlOptions, ['query' => ['page' => $this->first]])
    ); ?>">
        <span aria-hidden="true">&laquo;</span>
    </a>