blarg
blarg

Reputation: 3893

Slim Framework sub routing

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

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?

Upvotes: 2

Views: 227

Answers (1)

the-noob
the-noob

Reputation: 1342

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']
});

Upvotes: 2

Related Questions