The Slim Framework support forum has moved to http://discourse.slimframework.com. This Tender forum is no longer maintained or monitored.

Conditionally executing code on groups

richardcgeddes's Avatar

richardcgeddes

14 Sep, 2015 04:18 PM

I want to conditionally execute code on routing groups. Ideally something like this:

// Administrative Section
$app->group( '/admin', function () use ( $app ) {
    // conditionally execute code here for all administrative requests
    ...
    $app->get( '/routetarget1', function() use ( $app ) { ... }
    $app->get( '/routetarget2', function() use ( $app ) { ... }
}

or perhaps on "route middleware for groups":

function foo (){ ... }
// Administrative Section
$app->group( '/admin', foo(), function () use ( $app ) {
    $app->get( '/routetarget1', function() use ( $app ) { ... }
    $app->get( '/routetarget2', function() use ( $app ) { ... }
}

It seems like the only way I can achieve this is by applying route middleware to the actual route target:

function foo (){ ... }
// Administrative Section
$app->group( '/admin', function () use ( $app ) {
    $app->get( '/routetarget1', foo(), function() use ( $app ) { ... }
    $app->get( '/routetarget2', foo(), function() use ( $app ) { ... }
}

I'd like to have my whole app be rewritten (server rewrite) to index.php, so conditional execution might save unnecessary testing in app middleware, which always executes before/after the app or repetitively applying route middleware to the route targets in a group.

Is there a better way to do this with Slim 2?

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac