How to use routing to redirect to a page of a specific position?

I’m trying to use routing in order to route my home page to whatever page is in position 1 and I can’t figure out how to do that.

So for example, let’s say I have a unique page for each year so I have pages for 2018, 2017, 2016, etc. Each is in its corresponding position 2018=1, 2017=2, 2016=3 so that they appear in order in the navigation menu.

I want my home page to be whatever is in position 1. This year I want the home page to be the 2018 page. But when a new page is created for 2019, I want 2019 to be the home page.

What I have at the moment is the following code. It works, but obviously, I’ve hard coded that the home page should redirect to /2018 which isn’t ideal because when a new page for 2019 is created, the code will also manually have to be updated.

c:set('routes', array(
    array(
        'pattern' => '/'
        'action' => function() {
            return go('/2018');
        }
    )
));

How might I achieve what I’m looking to do?

You can get the page with number 1 like this:

$numberOne = $pages->findBy('num', '1');
go($numberOne);

Thanks Sonja. This seems so simple but I’m getting an error that $pages is undefined—I’m guessing because I’m trying to use your suggested code in my config.php file. Do you know how I might be able to use $pages in my config file?

Perhaps I’ve misunderstood what you meant but basically what I did with your code was put it into the function associated with the action in the routing code—basically replacing the line:

return go('/2018/)

Yes, sorry, of course.

$numberOne = site()->pages()->findBy('num', '1');

Ah yes that looks like it worked! Thank you so much!