+1 vote
by
For example, there is a link like this/контроллер/экшен/5 (for pagination EVERYTHING uses the format of links ending in page_number)

I assemble it this way for pagination:

$this->uri = dirname($_SERVER['REQUEST_URI']); // тут получаем /контроллер/экшен
$this->current_page = $this->getCurrentPage($current_page); // тут прилетает 5, проверяем-возвращаем

Conclusion
public function getHtml()
{
$start_page = $back = $page2left = $page1left = $page1right = $page2right = $forward = $end_page = null;
if ($this->current_page > 3) {
$start_page = " « ";
}
if ($this->current_page > 1) {
$back = " ‹ ";
}
if ($this->current_page - 2 > 0) {
$page2left = " " . ($this->current_page - 2) . " ";
}
if ($this->current_page - 1 > 0) {
$page1left = " " . ($this->current_page - 1) . " ";
}
if ($this->current_page + 1 <= $this->count_pages) {
$page1right = " " . ($this->current_page + 1) . " ";
}
if ($this->current_page + 2 <= $this->count_pages) {
$page2right = " " . ($this->current_page + 2) . " ";
}
if ($this->current_page < $this->count_pages) {
$forward = " › ";
}
if ($this->current_page < ($this->count_pages - 2)) {
$end_page = " » ";
}
return ' ';
} I have a question: this option withdirname($_SERVER['REQUEST_URI']) - is this the right solution? Is it right or is it some kind of quick fix? I just googled this solution to be honest, I didn't do dirname() with my own hands.

1 Answer

0 votes
by
 
Best answer
It would be better to use basename right away
echo basename('/контроллер/экшен/5').PHP_EOL; //5
echo basename('/контроллер/экшен/223/', '/').PHP_EOL; //223
...