1. Go to this page and download the library: Download deisss/slim-auth library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?phprequire_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
deisss / slim-auth example snippets
namespaceSlim\Extras\Middleware;
useSlim\Extras\Middleware\AbstractHTTPBasicAuthasAbstractHTTPBasicAuth;
/**
* Our concrete authentification implementation
*/classHTTPBasicAuthextendsAbstractHTTPBasicAuth{
/**
* Constructor
*
* @param array $skipUrl Any revelant path to skip authentification check
*/publicfunction__construct($skipUrl = null){
$this->setSkip($skipUrl);
}
/**
* The function to handle the database/session/facebook check
*
* @param string $login The login supply by user
* @param string $password The password supply by user
* @param string $path The url user try to access
* @return Any value 'null' for php empty function will be consider
* as a fail (and yet be refused), any non-empty value
* will be accepted
*/protectedfunctiongetUserFromLogin($login, $password, $path){
// Your database/session check here// Any non-empty/false value will be consider as 'ok', we// recommand to send back full user object (as you can recover it later into route function - see below)returntrue;
}
}
re 'HTTPBasicAuth.php';
$app = new \Slim\Slim();
$app->add(new \Slim\Extras\Middleware\HTTPBasicAuth(array(
'/hello/:name'
)));
$app->get('/hello/:name', function($name)use($app){
echo'Hello '.$name;
});
$app->get('/logged', function()use($app){
$userFromAuth = $app->request()->headers('auth');
// Same
$userFromAuth = $app->request()->headers('user');
});
$app->run();
functionisAdministrator(){
$app = \Slim\Slim::getInstance();
// userFromAuth is now a $_SESSION array instead of boolean value
$userFromAuth = $app->request()->headers('auth');
// We test, and refuse if the role is not OKif($userFromAuth['role'] != 'administrator') {
$app->status(403);
$app->stop();
}
}
$app->get('/this-is-acl', 'isAdministrator', function(){
});
HTTPBasicAuth.php
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.