Support the ongoing development of Laravel.io →
Authentication Validation Architecture

Hi guys!

Recently I added a "page" segment on the url for pagination so I changed my '{section}' route to '{section}/{page?}/{pageCount?}. I wanted it to be optional so /games will just point to the first page and /games/page/2 will point to the second page. The problem is adding { } will cause it to be passed on a controller function so now my controller function looks like this:

function index($section, $page, $pageCount){
    // do something here
}

I only want to pass the section and the pageCount in the controller function and the other problem is how can I set a default value for the pageCount when I'm using this kind of route setting

Route::get(
    '/{section}', 
    array(
        'as'   => 'route.post',
        'uses' => 'PostController@index',
    )
)
->where(
    array(
        'section' => '(games|guides|cheats)'
    )
);

Thanks in advance!

Last updated 3 years ago.
0

Set up two routes, one without pagination and another with it.

Route::get( '{section}', array(
        'as'   => 'route.post',
        'uses' => 'PostController@index',
))->where(array(
        'section' => '(games|guides|cheats)'
));

Route::get( '{section}/page/{pageCount}', array(
        'as'   => 'route.post.paged',
        'uses' => 'PostController@index',
))->where(array(
        'section' => '(games|guides|cheats)'
));

then in your controller define the default pageCount

function index($section, $pageCount = 1){
    // do something here
}
Last updated 3 years ago.
0

@spescina Toying with routes yesterday I ended up with this kind of solution, glad to know that someone else ended up with this solution. Thanks!

Last updated 3 years ago.
0

Sign in to participate in this thread!

PHPverse

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.