2

I want to use sub routes with Slim Framework v3.2.0 like so:

  • www.test.com/ <-- index page
  • www.test.com/foodtype/ <-- separate page
  • www.test.com/foodtype/page/ <-- subcategory of foodtype

As I understand it, only one get can be called. Currently I have this in my routes.php:

$app->get('/', function () {
// Load index page
});

$app->get('/{foodtype}', function ($request, $response, $args) {
// Load page based on the value of $args['foodtype'] 
});

How can I add the separate optional route for page1?

I've tried:

$app->get('/{foodtype}/{page}', function ($request, $response, $args) {
// Load page based on the value of $args['foodtype'] and $args['page']
});

This causes a 'page not found' error. I presume I need to escape the optional '/' too?

alexw
  • 8,468
  • 6
  • 54
  • 86
blarg
  • 3,773
  • 11
  • 42
  • 71

1 Answers1

2

You will have to make the page part optional in your original route.

As in:

$app->get('/{foodtype}', function ($request, $response, $args) {
// Load page based on the value of $args['foodtype'] 
});

becomes:

$app->get('/{foodtype}[/{page}]', function ($request, $response, $args) {
// Load page based on the value of $args['foodtype'] and $args['page']
});
the-noob
  • 1,322
  • 2
  • 14
  • 19