如何使用cakephp在url中传递param

时间:2016-08-05 11:31:04

标签: cakephp

我正在使用cakephp 2.6.7。我想在url中传递param。我的预期网址是:http://demo.jegeachi.com/tolets/search?page=2

但是url看起来像: http://demo.jegeachi.com/tolets/search/%2526page%253D2

我的代码是:

 if ($total_page > 2):
                    $current_page = 0;
                    if(isset($this->params['url']['page'])){
                      $current_page = $this->params['url']['page'];
                    }
                        ?>
                       <?php if($current_page>1){
                         $url = 'page='.--$current_page;
                        ?>
                       <li><a href="<?php echo Router::url(array('controller' => 'tolets', 'action' => 'search', $url)); ?>"> &laquo;</a></li>
                      <?php }?> 

                        <?php for ($page = 1; $page <= $total_page; $page++):
                            ?>
                            <?php if ($page == $current_page) { ?>
                                <li><span><?php echo $page; ?> </span></li>
                            <?php } else {
                               $url = '&page='.$page;
                             ?>
                                <li><a href="<?php echo Router::url(array('controller' => 'tolets', 'action' => 'search', $url)); ?>"><?php echo $page; ?></a></li>
                            <?php } ?>
                        <?php endfor;
                        ?>
                        <?php if($current_page<$total_page){
                        $url = 'page='.++$current_page;
                        ?>
                        <li><a href="<?php echo Router::url(array('controller' => 'tolets', 'action' => 'search', $url)); ?>">&raquo;</a></li>
                        <?php } ?>

                        <?php endif;
                        ?>

我也试过使用urlencode但没有运气。

2 个答案:

答案 0 :(得分:0)

如果这是一个视图代码

<?php
echo $this->Html->link('Title', array(
    'controller' => 'tolets',
    'action' => 'search',
    '?' => array('page' => 2))
);
?>

将输出

<a href="/tolets/search?page=2">Title</a>

答案 1 :(得分:0)

你也可以使用

<?php
echo $this->Html->link('Title', array(
    'controller' => 'tolets',
    'action' => 'search','?page=2')
);
?>