【问题标题】:ZF2 pagination Passing Value From RouteZF2 分页从路由传递值
【发布时间】:2017-01-04 20:39:43
【问题描述】:

在我的 Zend Framework 项目中,我有一个“基础资产”模块。在此我正在尝试创建一个与以下路线匹配的分页:

'route'    => '/video/search[/:search][/:page_reference]',

搜索值的目的是,如果用户点击任何创建的“下一页”页面链接,相同的查询将在数据库上运行。

page_reference 的值工作正常。我不确定如何为分页填充 search 的值:

<li>
    <a href="<?php echo $this->url(
        $this->route,
        ['page_reference' => $this->previous , 'search' => $this->search]
    ); ?>"> << </a>
</li>

以下尝试均未成功:

$this->search
$this->params()->fromRoute('search')

从视图调用分页使用

{{ paginationControl(result, 'sliding', ['actsministries/asset/searchpaginator', 'AMBase']) }}

我应该在服务中设置搜索值吗?

【问题讨论】:

    标签: php zend-framework pagination


    【解决方案1】:

    1 - 创建视图助手:

    use Zend\View\Helper\AbstractHelper;
    
    class Requesthelper extends AbstractHelper
    {
        protected $request;
    
        //get Request
        public function setRequest($request)
        {
            $this->request = $request;    
        }
    
        public function getRequest()
        {
            return $this->request;    
        }
    
        public function __invoke()
        {
            return $this->getRequest()->getServer()->get('QUERY_STRING');     
        }
    }
    

    2 - 模块.php

    public function getViewHelperConfig()
        {
            return array(
                'invokables' => array(
                ),
    
                'factories' => array(
                    'Requesthelper' => function($sm){
                       $helper = new YourModule\View\Helper\Requesthelper; \\the path to creatd view helper
                       $request = $sm->getServiceLocator()->get('Request');
                       $helper->setRequest($request);
                       return $helper;
                    }   
               ),
            );   
        }
    

    3 - searchpaginator.phtml:

        <?php 
    $parameterGet = $this->Requesthelper();
    if ($parameterGet != ""){
        $aParams = explode("&", $parameterGet);
        foreach($aParams as $key => $value){
            if(strpos($value, "page_reference=") !== false){
                unset($aParams[$key]);
            }
        }
    
        $parameterGet = implode("&", $aParams);
    
        if($parameterGet != '')
            $parameterGet = "&".$parameterGet;
    }
    
    ?>
    <?php if ($this->pageCount):?>
         <div>
             <ul class="pagination">
                 <!-- Previous page link -->
                 <?php if (isset($this->previous)): ?>
                     <li>
                         <a href="<?php echo $this->url($this->route); ?>?page_reference=<?php echo $this->previous.$parameterGet; ?>">
                             &lt;&lt;
                         </a>
                     </li>
                 <?php else: ?>
                     <li class="disabled">
                         <a href="#">
                             &lt;&lt;
                         </a>
                     </li>
                 <?php endif; ?>
    
                 <!-- Numbered page links -->
                 <?php foreach ($this->pagesInRange as $page): ?>
                     <?php if ($page != $this->current): ?>
                         <li>
                             <a href="<?php echo $this->url($this->route);?>?page_reference=<?php echo $page.$parameterGet; ?>">
                                 <?php echo $page; ?>
                             </a>
                         </li>
                     <?php else: ?>
                         <li class="active">
                             <a href="#"><?php echo $page; ?></a>
                         </li>
                     <?php endif; ?>
                 <?php endforeach; ?>
    
                 <!-- Next page link -->
                 <?php if (isset($this->next)): ?>
                     <li>
                         <a href="<?php echo $this->url($this->route); ?>?page_reference=<?php echo $this->next.$parameterGet; ?>">
                             &gt;&gt;
                         </a>
                     </li>
                 <?php else: ?>
                     <li class="disabled">
                         <a href="#">
                             &gt;&gt;
                         </a>
                     </li>
                 <?php endif; ?>
             </ul>
         </div>
     <?php endif; ?>
    

    4 - index.phtml

    <?php
     // add at the end of the file after the table
     echo $this->paginationControl(
         // the paginator object
         $this->paginator,
         // the scrolling style
         'sliding',
         // the partial to use to render the control
         'partial/searchpaginator.phtml',
         // the route to link to when a user clicks a control link
         array(
             'route' => '/video/search'
         )
     );
    ?>
    

    我希望这就是你想要的

    【讨论】:

    • 辉煌。谢谢!
    猜你喜欢
    • 2014-09-05
    • 2016-05-05
    • 1970-01-01
    • 2016-12-08
    • 2015-01-14
    • 2013-03-23
    • 2011-10-28
    • 1970-01-01
    • 2018-04-02
    相关资源
    最近更新 更多