dydx's avatar
Level 5

Weird pagination issue and non-root URLs

I'm having a very strange problem with pagination with non-root URLs.

The URL: http://local.dev/?page=3 is paginated correctly, showing the correct bit of database info for page 3.

The URL: http://local.dev/test/?page=3 is not paginated correctly, while the URL does suggest it's calling the right information, the page is still displaying the information for page 1.

Before I narrowed it down to being related to the URL, I was commenting out everything from my project, models, views, view composers... everything. Then I tried presenting the same information as I would normally have displayed on the /test/ page on the / page, and everything worked OK.

My routes.php looked this right now:

Route::get('/', function() {
  $work_orders = App\WorkOrder::paginate(2);
  return $work_orders->render();
});

Route::get('/test', function() {
  $work_orders = App\WorkOrder::paginate(2);
  return $work_orders->render();
});

I'm really not sure what to do to fix this.

0 likes
5 replies
nicogominet's avatar

I had the same issue but I got it fixed by checking my Nginx configuration, where I had :

try_files $uri $uri/ /index.php;

instead of

try_files $uri $uri/ /index.php?$query_string;

In my case, the page parameter wasn't passed on urls when not on the home page ('/').

Hope this helps!

bestmomo's avatar

I always delete this troublesome backslash :

$links = str_replace('/?', '?', $somethings->render());
PedroM's avatar

did you try using your route without the leading trailing slash?

Route::get('test', function() {
  $work_orders = App\WorkOrder::paginate(2);
  return $work_orders->render();
});
dydx's avatar
Level 5

hmmmm. Here's the nginx file I've been using for this:

server {
  server_name default;
  root        /var/www/default/public;
  index       index.php;

  client_max_body_size 100M;
  fastcgi_read_timeout 1800;

  location / {
    try_files $uri $uri/ /index.php$query_string;
  }

  if (!-d $request_filename) {
    rewrite ^(.+)/$ /$1 permanent;
  }

  location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
    expires       max;
    log_not_found off;
    access_log    off;
  }

  location ~ \.php$ {
    try_files                 $uri =404;
    include                   fastcgi_params;
    fastcgi_index             index.php;
    fastcgi_split_path_info   ^(.+\.php)(.*)$;
    fastcgi_param             SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_pass              127.0.0.1:9000;
  }
}

So, I am passing the $query_string arg through the / location. I have tried the route without a backslash, and I have tried manually removing the backslash with str_replace, but no banana.

I've been looking at just using the spatie/laravel-routepaginator lib, which does actually work (and gives 'pretty' urls), but I don't think I should have to do that. Just unsure what the problem is.

dydx's avatar
Level 5

On a whim I implemented a Resource Controller (with index, show, destroy, etc) and then added a new Route Resource:

Route::resource('tickets', 'PagesController');

And pagination seems to work out pretty well with that. I'm sure there was something wrong with how I was doing it the other way, which is still mildly confusing (I like finding a solution, but not understanding why the old way was broken is frustrating).

I think I'm just going to refactor a bit around this new resource controller and route resource, since its working (and looks a bit cleaner, as well).

Please or to participate in this conversation.