# Slim Framework 3 skeleton application has authentication MVC construction

**URL:** <https://discourse.slimframework.com/t/slim-framework-3-skeleton-application-has-authentication-mvc-construction/2088>\
**Category:** Questions\
**Created:** [December 12, 2017, 3:10am UTC](https://discourse.slimframework.com/t/slim-framework-3-skeleton-application-has-authentication-mvc-construction/2088 "2017-12-12T03:10:00Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![HavenShen](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/havenshen/32/578_2.png) [@HavenShen](https://discourse.slimframework.com/u/HavenShen)\
**Post date:** [December 12, 2017, 3:10am UTC](https://discourse.slimframework.com/t/slim-framework-3-skeleton-application-has-authentication-mvc-construction/2088/1 "2017-12-12T03:10:00Z")

</div>

## Slim Framework 3 skeleton application has authentication MVC construction.

> Github: [https://github.com/HavenShen/slim-born](https://github.com/HavenShen/slim-born)

The user authentication has been achieved, there is a need to install and use.

## Installation

```shell
composer create-project havenshen/slim-born [my-app]

```

## .env

Copy file .env.example to .env

```auto
DB_DRIVER=mysql
DB_HOST=localhost
DB_DATABASE=slimborn
DB_USERNAME=root
DB_PASSWORD=
DB_PORT=3306

```

## Router

This is a Slim Framework Router.

```php
<?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));

```

## Controller

Use Slim Framework Twig View.

```php
<?php

namespace App\Controllers;

class HomeController extends Controller
{
	public function index($request, $response)
	{
		return $this->view->render($response, 'home.twig');
	}
}

```

## Model

Use Laravel PHP Framework Eloquent.

```php
<?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)
		]);
	}
}

```

## Middleware

```php
<?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;
	}
}

```

## Validation

Use the most awesome validation engine ever created for PHP.

```php
<?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
	}
}

```

## Directory Structure

```shell
|-- slim-born
	|-- app
		|-- Auth
		|-- Controllers
		|-- Middleware
		|-- Models
		|-- Validation
		|-- Routes.php
	|-- bootstrap
		|-- app.php
	|-- public
	|-- resources
	....

```
