Slim Framework 3 or 4 skeleton application has authentication MVC construction.
The user authentication has been achieved, there is a need to install and use.
composer create-project "havenshen/slim-born v1.0.6" [my-app]
composer create-project "havenshen/slim-born 2.0" [my-app]
Copy file .env.example to .env
DB_DRIVER=mysql
DB_HOST=localhost
DB_DATABASE=slimborn
DB_USERNAME=root
DB_PASSWORD=
DB_PORT=3306
This is a Slim Framework Router. Reference - Slim Router
<?php
$app->get('/', 'HomeController:index')->setName('home');
$app->group('', function () {
$this->get('/auth/signup', 'AuthController:getSignUp')->setName('auth.signup');
$this->post('/auth/signup', 'AuthController:postSignUp');
$this->get('/auth/signin', 'AuthController:getSignIn')->setName('auth.signin');
$this->post('/auth/signin', 'AuthController:postSignIn');
})->add(new GuestMiddleware($container));
Use Slim Framework Twig View. Reference - Twig-View
<?php
namespace App\Controllers;
class HomeController extends Controller
{
public function index($request, $response)
{
return $this->view->render($response, 'home.twig');
}
}
Use Laravel PHP Framework Eloquent. Reference - illuminate/database
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
protected $table = 'users';
protected $fillable = [
'email',
'name',
'password',
];
public function setPassword($password)
{
$this->update([
'password' => password_hash($password, PASSWORD_DEFAULT)
]);
}
}
<?php
namespace App\Middleware;
class AuthMiddleware extends Middleware
{
public function __invoke($request, $response, $next)
{
if(! $this->container->auth->check()) {
$this->container->flash->addMessage('error', 'Please sign in before doing that');
return $response->withRedirect($this->container->router->pathFor('auth.signin'));
}
$response = $next($request, $response);
return $response;
}
}
Use the most awesome validation engine ever created for PHP. Reference - Respect/Validation
<?php
namespace App\Controllers\Auth;
use App\Models\User;
use App\Controllers\Controller;
use Respect\Validation\Validator as v;
class AuthController extends Controller
{
public function postSignUp($request, $response)
{
$validation = $this->validator->validate($request, [
'email' => v::noWhitespace()->notEmpty()->email()->emailAvailable(),
'name' => v::noWhitespace()->notEmpty()->alpha(),
'password' => v::noWhitespace()->notEmpty(),
]);
if ($validation->failed()) {
return $response->withRedirect($this->router->pathFor('auth.signup'));
}
// todo someting
}
}
reference slim official documents - Slim Framework
- illuminate/database - It also serves as the database layer of the Laravel PHP framework.
- Respect/Validation - The most awesome validation engine ever created for PHP.
- slimphp/Slim - Slim Framework created.
- slimphp/Slim-Csrf - Slim Framework created.
- slimphp/Twig-View - Slim Framework created.
- slimphp/Slim-Flash - Slim Framework created.
|-- slim-born
|-- app
|-- Auth
|-- Controllers
|-- Middleware
|-- Models
|-- Validation
|-- Routes.php
|-- bootstrap
|-- app.php
|-- public
|-- resources
....
$ phpunit
Feel free to submit any minor enhancements, the goal here is to keep this as simple (yet as practical) as possible.
The MIT License (MIT). Please see License File for more information.