Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Pagination and Routes
  • Hello, I'm having come issues with figuring out how to do routes and whatnot for a paginated webpage. Here are the example routes: 'page/(:segment)' => 'controller/index/$1',
    'page' => 'controller/index', Ideally these two could be one line - clogging up routing files for every time something like this occurs would be messy, to say the least. However, the links generated by the pagination class give the 1st page a link of "controller/index" Aka "page", without any /(:segment) type of thing. This triggers an error which I have yet to find a way around without simply rerouting to the base. So, there are two ideal solutions I see:
    1. Some way I may not be aware of to write the route config line so that the [/(:segment)] and "$1" are optional
    2. Some way to alter the pagination config so that the code generated is page/1 as opposed to /page. I can think of a few dirty ways to accomplish this, but I was wondering if there were any best practices/recommendations out there. Also I'm thinking :segment should probably be :num - would that be accurate? Thanks.
  • You can configure the pagination class through it's configuation:
    // set pagination information
    $pagination['current_page'] = $page;
    $pagination['per_page'] = 20;
    $pagination['num_links'] = 10;
    $pagination['total_items'] = Model_Something::count();
    $pagination['pagination_url'] = \Uri::create('controller/index');
    $pagination['uri_segment'] = 3;
    \Pagination::set_config($pagination);
    

    This is all documented. So set the 'pagination_url' to \Uri::create( 'page' ), set 'uri_segment' to 2 ( to create page/<num> ) and you're in business. As for routes, these are standard regexes, so you can do things like 'page(\:num)?'. As something in brackets count as a section to split off, you probably have to use $2 to catch your :num placeholder.
  • The regex is the piece I was missing. Thank you. Tony

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion