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

Allow Class:Action syntax for route middleware

Stefan Melbinger's Avatar

Stefan Melbinger

27 Apr, 2014 01:36 PM

Most of my routes looks something like this:

$app->get('/blah/:id',
    function() use ($app) { $app->myLoginMiddleware->checkIfLoggedIn(); },
    function() use ($app) { $app->myUserRightsMiddleware->checkIfAdministrator(); },
    '\path\to\my\Controller:doSomething')
    ->name('blah');

This defines two route middlewares to be executed before routing the request to the actual action, doSomething(). I really like the Class:Action syntax that can be used for the action route instead of a callback function.

My suggestion is to make this syntax also available to point to route middleware code. This would make my code a lot more readable:

$app->get('/blah/:id',
    '\path\to\my\LoginMiddleware:checkIfLoggedIn',
    '\path\to\my\UserRightsMiddleware:checkIfAdministrator',
    '\path\to\my\Controller:doSomething')
    ->name('blah');

I'm looking forward to hearing your thoughts.

  1. Support Staff 1 Posted by Andrew Smith on 28 Apr, 2014 09:53 PM

    Andrew Smith's Avatar

    I am of the opinion that if you need something like this, it might be better to create functions that wraps those namespaced middlewares.

    $checkIfLoggedIn = function () {
        return \MyMiddleware\LoginMiddleware::checkIfLoggedIn();
    };
    
    $app->get('/blah/:id', $checkIfLoggedIn, $checkIfAdministrator, 'MyNamespace\Controller:doSomething')->name('blah');
    
  2. 2 Posted by Stefan Melbinge... on 30 Apr, 2014 07:24 AM

    Stefan Melbinger's Avatar

    Thanks for your suggestion, Andrew. However, this simply creates even more lines of code and clutters up my bootstrap code even more.

    I would strongly suggest looking at additional ways to invoke route middleware (as suggested above) just like route actions can be invoked through anonymous functions as well as Class:Action strings (and other "callable" entities).

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