Return to current page after passing data to controller

I have a route defined in config.php that currently checks the URL pattern to see if it has /landscape or /portrait on the end, then passes the appropriate value to the page before passing it along. I need this to work here as there are several places it is used where the detected orientation is wrong when using HTML/CSS/JS, or those detection methods are unsupported. Every time I try to get help with this, everyone gets hung up on why, and forgets to answer how, so there’s the why.

The following works, but it hard-set to a single page. I need this function to pass the data to the controller but to the current page, no matter it’s name. I attempted to do this with return page($page)->render($data); but it returned an error that $page was undefined. This is confusing because I see other examples where $page is used to return the current page in config.php.

here is the full router code:

  'routes' => [
    [
        'pattern' => 'slideshows/(:any)/landscape',
        'action' => function ($value) {
            $data = [
              'orientation' => $value,
            ];
            return page('slideshows/selfcheck')->render($data);
        }
    ],
    [
      'pattern' => 'slideshows/(:any)/portrait',
      'action' => function ($value) {
          $data = [
            'orientation' => $value,
          ];
          return page('slideshows/selfcheck')->render($data);
      }
    ]
  ]

All I need is to check the URL for landscape or portrait, then set the appropriate value, so if there’s a better way to do that in config.php, i’m happy to do that.

Here you rightly set the (:any) placeholder, this should be for any of the subpages of slideshows, but then when returning the page, you hardcode the subpage to selfcheck instead of using this placeholder. Also, your pass the subpage placeholder value to orientation, so instead of portrait or landscape, you will have as value of the subpage slug used.

One route with two placeholders (one for the subpage name, one for the orientation) should be sufficient:

  [
      'pattern' => 'slideshows/(:any)/(:any)',
      'action' => function ($subpage, $orientation) {
          $data = [
            'orientation' => $orientation,
          ];
          return page('slideshows/' . $subpage)?->render($data);
      }
    ]

THANK YOU! This is exactly what I was looking for!

I’m still learning PHP so I wasn’t entirely sure what was doing what, I was just using the docs to try and learn the router options, so your explanation was extremely helpful in understanding how to fix it!