Slim Framework is Micro Framework Simple code and very powerful web applications and Api.
Micro Framework you can use this middleware to implement HTTP Basic Authentication.By default middleware authenticates array of username and password. You can also againt database or any other datasource by providing custom authenticator.
Slim Framework Simple Login Authentication Code and Session
<?php
session_cache_limiter(false);
session_start();
require 'class/Slim/Slim.php';
//$app = new SessionCookie();
\Slim\Slim::registerAutoloader();
$pos = strpos( $_SERVER['PHP_SELF'], '/index.php');
global $Url;
$Url = substr( $_SERVER['PHP_SELF'], 0, $pos);
require 'template/View.php';
$View = new View();
$app = new \Slim\Slim([
//'templates.path' => 'assets',
'templates.path' => 'template','debug'=>true,
'view' => $View
]);
$app->add(new \Slim\Middleware\SessionCookie(array('secret' => 'myappsecret')));
$app->get('/',function()use ($app){
if(empty($_SESSION['username']))
{
$login = strpos( $_SERVER['PHP_SELF'], '/index.php');
$baseUrl = substr( $_SERVER['PHP_SELF'], 0, $login);
$app->render('Login.php',array('baseUrl' => $baseUrl ));
}
else{
if(isset($_SESSION['secret']))
{
$app1 =(Apps::find_by_client_secret($_SESSION['secret']));
$token = Authtoken::find_by_user_id_and_app_id($_SESSION['id'],$app1->app_id);
if(!empty($token->authorization_code))
{
$_SESSION['secret']=null;
$app->response->redirect($app1->redirect_url.'?token='.$token->authorization_code);
}
}else{
$app->response->redirect('addapp');}
}
});
$routeHelper = function (\Slim\Route $route) {
echo "Current route is " . $route->getName();if (!isset($_SESSION['username'])) {
$_SESSION['urlRedirect'] = $app->request()->getPathInfo();
$app->flash('error', 'Login required');
// $app->redirect($Url);
}
};
$user = function ($app) {
return function () use ($app) {
global $Url;
// echo "Current route is " . $route->getCurrentRoute();
if (!isset($_SESSION['username'])) {
$_SESSION['urlRedirect'] = $app->request()->getPathInfo();
$app->flash('error', 'Login required');
$app->redirect($Url);
}
};
};
Slim Framework View Specify Template or Layout
$View->setLayout('layout.php');
Slim Framework Simple Registration Model code:
$app->post('/signup',function()use ($app){
//echo 'Hai Im Work Us Slim Framework';
global $Url;
$reg = new Client();
$username = $app->request()->post('username');
$reg->firstname = $app->request()->post('firstname');
$reg->lastname = $app->request()->post('lastname');
$reg->email = $app->request()->post('email');
$reg->username = $app->request()->post('username');
$reg->password = $app->request()->post('password');
//$reg->confirmpassword = $app->request()->post('confirmpassword');
$reg->dob = $app->request()->post('dob');
$reg->gender = $app->request()->post('gender');
$reg->mobilenumber = $app->request()->post('mobilenumber');
$reg->country = $app->request()->post('country');
if($reg->is_valid())
{
$reg->save();
}
else
{
$app->flash('error', 'dob '.$reg->errors->on('dob'));
//$app->flash('error', 'email '.$reg->errors->on('email'));
//$app->flash('error', 'mobilenumber '.$reg->errors->on('mobilenumber'));
$app->response->redirect($Url);
}
});