Search code examples
routerauraphp

PHP (AuraPHP router) - routing with more than one optional parameters


i have got a little problem in my auraphp router. For example i have this URL : oleje/134/motorove-oleje-pro-automobily/

But i can have this URl also : oleje/134/motorove-oleje-pro-automobily/oleje-shell

The "oleje-shell" part of URL is optional and i need to put behind this part one more optional parameter called "per_page". So i need something like : oleje/134/motorove-oleje-pro-automobily/35 or : oleje/134/motorove-oleje-pro-automobily/oleje-shell/35.

I hope, you got it.

Thanks for help.


Solution

  • 2 options are coming to my mind.

    1. Optional Params

      $router->add('archive', '/oleje/134/motorove-oleje-pro-automobily{/oleje,number}');

    2. Wild card Params

      $router->add('wild_post', '/oleje/134/motorove-oleje-pro-automobily/') ->setWildcard('other');

      // this matches, with the following values $route = $router->match('/oleje/134/motorove-oleje-pro-automobily/oleje-shell/35', $_SERVER); // $route->params['other'] = array('oleje-shell', '35')

    In both cases you may want to check whether the one you got is string or number.

    Hope that help!