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

Set Middleware Function Visible on all files

domingo's Avatar

domingo

15 Jun, 2015 06:53 AM

How could I set a function to be visible on every file of the project.
I mean, for example if I have something like this in index.php

$authenticateForRole = function ( $role = 'member' ) {
    return function () use ( $role ) {
        $user = User::fetchFromDatabaseSomehow();
        if ( $user->belongsToRole($role) === false ) {
            $app = \Slim\Slim::getInstance();
            $app->flash('error', 'Login required');
            $app->redirect('/login');
        }
    };
};
$app = new \Slim\Slim();

require 'routes/products.php';

The file products.php doesn't notice there's a function called authenticationForRole

  1. Support Staff 1 Posted by Andrew Smith on 15 Jun, 2015 01:04 PM

    Andrew Smith's Avatar

    This should be visible in your include files as long as it was declared before the include. Also you might want to move that function below $app = new \Slim\Slim;, since you are making use of $app inside of the function.

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