Advertisement
MatvikX

Untitled

Apr 24th, 2024
767
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.51 KB | Software | 0 0
  1. <?php
  2.  
  3. namespace App\Components;
  4.  
  5. use Illuminate\Pagination\LengthAwarePaginator;
  6. use Illuminate\Support\Arr;
  7.  
  8. class SeoPaginator extends LengthAwarePaginator
  9. {
  10.     /**
  11.      * Remove page parameter from the first page URL.
  12.      * Removing URL from active page.
  13.      *
  14.      * @param [type] $page
  15.      * @return void
  16.      */
  17.     public function url($page)
  18.     {
  19.         if ($page <= 0) {
  20.             $page = 1;
  21.         }
  22.  
  23.         if ($this->currentPage == $page) {
  24.             return null;
  25.         }
  26.  
  27.         // If we have any extra query string key / value pairs that need to be added
  28.         // onto the URL, we will put them in query string form and then attach it
  29.         // to the URL. This allows for extra information like sortings storage.
  30.         $parameters = [$this->pageName => $page];
  31.  
  32.         if (count($this->query) > 0) {
  33.             $parameters = array_merge($this->query, $parameters);
  34.         }
  35.  
  36.         // Remove 'page' parameter if it's 1
  37.         if ($page == 1 && isset($parameters['page'])) {
  38.             unset($parameters['page']);
  39.         } else {
  40.             $parameters['page'] = $page;
  41.         }
  42.  
  43.         $result =  $this->path()
  44.             . (count($parameters) > 0
  45.                 ? (str_contains($this->path(), '?') ? '&' : '?')
  46.                 : '')
  47.             . Arr::query($parameters)
  48.             . $this->buildFragment();
  49.  
  50.         return trim($result, '?&');
  51.     }
  52. }
  53.  
  54. // In AppServiceProvider
  55. $this->app->alias(SeoPaginator::class, LengthAwarePaginator::class);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement