CakePHP分页,如何使当前页面成为锚点

In my view I am using this code:

$numbers = $this->Paginator->numbers(array(
    'separator'     => '',
    'tag'           => 'li',
    'currentClass'  => 'active'
));

which outputs:

<li class="active">1</li>
<li><a href="/controller/action/page:2">2</a></li>
<li><a href="/controller/action/page:3">3</a></li>

This works pretty well, the only issue I have with it is that the current page is not a link. Is it possible to make the current page a link?

Thanks for reading.

I implemented a class extension like Dave suggested, but instead of copying all the code from the original class, I did a string replace method instead, that way, if I update the CakePHP core library, this should fail pretty gracefully, where as copying all the code from the original Helper may cause loss of features, bug fixes, etc. Here is the class that I implemented:

<?php

class AppPaginatorHelper extends PaginatorHelper
{
    public function numbers($options = array()) {
        $output = parent::numbers($options);

        // get the current page number, and create a link with it
        $current = $this->current();
        $currentLink = $this->link($current, array('page' => $current));

        // if you're using cake pre 2.1 you cannot change the current class with
        // the options array, so it will always be "current"
        $find = "<li class=\"current\">{$current}</li>";
        $replace = "<li class=\"active\">{$currentLink}</li>"; 

        $output = str_replace($find, $replace, $output);

        return $output;
    }
}

Simple answer:

I don't believe it's available with the Paginator Helper.

If you just want a link to the current page but don't need it within the numbered links, you could use

echo $this->Html->link($this->Paginator->counter('{:current}'), 'yourLinkHere');

But that's not incredibly helpful, since you're relying on the Paginator Helper to take care of the rest of the links for you.

Extended answer / possibility

You could extend the PaginatorHelper with something like this below. Basically, I just removed the check to see if it's the current page number. Then you'd have to use MyPaginatorHelper to build the links instead. This would also make it ignore the currentClass option...etc. But - with some more tweaking of the code, you could just make it so it does the same thing but also builds a link instead of just removing the IF check.

class MyPaginatorHelper extends PaginatorHelper {
    public function numbers($options = array()) {
    if ($options === true) {
        $options = array(
            'before' => ' | ', 'after' => ' | ', 'first' => 'first', 'last' => 'last'
        );
    }

    $defaults = array(
        'tag' => 'span', 'before' => null, 'after' => null, 'model' => $this->defaultModel(), 'class' => null,
        'modulus' => '8', 'separator' => ' | ', 'first' => null, 'last' => null, 'ellipsis' => '...', 'currentClass' => 'current'
    );
    $options += $defaults;

    $params = (array)$this->params($options['model']) + array('page' => 1);
    unset($options['model']);

    if ($params['pageCount'] <= 1) {
        return false;
    }

    extract($options);
    unset($options['tag'], $options['before'], $options['after'], $options['model'],
        $options['modulus'], $options['separator'], $options['first'], $options['last'],
        $options['ellipsis'], $options['class'], $options['currentClass']
    );

    $out = '';


        $half = intval($modulus / 2);
        $end = $params['page'] + $half;

        if ($end > $params['pageCount']) {
            $end = $params['pageCount'];
        }
        $start = $params['page'] - ($modulus - ($end - $params['page']));
        if ($start <= 1) {
            $start = 1;
            $end = $params['page'] + ($modulus - $params['page']) + 1;
        }

        if ($first && $start > 1) {
            $offset = ($start <= (int)$first) ? $start - 1 : $first;
            if ($offset < $start - 1) {
                $out .= $this->first($offset, compact('tag', 'separator', 'ellipsis', 'class'));
            } else {
                $out .= $this->first($offset, compact('tag', 'separator', 'class') + array('after' => $separator));
            }
        }

        $out .= $before;

        for ($i = $start; $i < $params['page']; $i++) {
            $out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options), compact('class')) . $separator;
        }

        if ($class) {
            $currentClass .= ' ' . $class;
        }
        $out .= $this->Html->tag($tag, $params['page'], array('class' => $currentClass));
        if ($i != $params['pageCount']) {
            $out .= $separator;
        }

        $start = $params['page'] + 1;
        for ($i = $start; $i < $end; $i++) {
            $out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options), compact('class')) . $separator;
        }

        if ($end != $params['page']) {
            $out .= $this->Html->tag($tag, $this->link($i, array('page' => $end), $options), compact('class'));
        }

        $out .= $after;

        if ($last && $end < $params['pageCount']) {
            $offset = ($params['pageCount'] < $end + (int)$last) ? $params['pageCount'] - $end : $last;
            if ($offset <= $last && $params['pageCount'] - $end > $offset) {
                $out .= $this->last($offset, compact('tag', 'separator', 'ellipsis', 'class'));
            } else {
                $out .= $this->last($offset, compact('tag', 'separator', 'class') + array('before' => $separator));
            }
        }

    }

    return $out;
}
}

There's a PR made to make it work. Unfortunately it's just going to happen in 2.3 version.

Here you can find the PR https://github.com/cakephp/cakephp/pull/900

Here you can find the Discussion http://cakephp.lighthouseapp.com/projects/42648/tickets/2892-paginator-helper-numbers-is-a-bit-counter-intuitive-enhancement-included

You can make what you want by using the next piece of code:

<?php echo $this->Paginator->numbers(array('separator' => '','tag' => 'li', 'currentTag' => 'a', 'currentClass' => 'active')); ?>

It only works for CakePHP 2.3+

I find a good solution and share it for you :)

<div class="pagination pagination-large">
    <ul>
            <?php
                echo $this->Paginator->prev(__('prev'), array('tag' => 'li'), null, array('tag' => 'li','class' => 'disabled','disabledTag' => 'a'));
                echo $this->Paginator->numbers(array('separator' => '','currentTag' => 'a', 'currentClass' => 'active','tag' => 'li','first' => 1));
                echo $this->Paginator->next(__('next'), array('tag' => 'li','currentClass' => 'disabled'), null, array('tag' => 'li','class' => 'disabled','disabledTag' => 'a'));
            ?>
        </ul>
</div>