PHP code example of hezecom / slim-starter

1. Go to this page and download the library: Download hezecom/slim-starter library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hezecom / slim-starter example snippets




$app->get('/', 'HomeController:index')->setName('home');

$app->group('', function ($route) {
    $route->get('/register', AuthController::class . ':createRegister')->setName('register');
    $route->post('/register', AuthController::class . ':register');
    $route->get('/login', AuthController::class . ':createLogin')->setName('login');
    $route->post('/login', AuthController::class . ':login');

    $route->get('/verify-email', AuthController::class.':verifyEmail')->setName('verify.email');
    $route->get('/verify-email-resend',AuthController::class.':verifyEmailResend')->setName('verify.email.resend');

    $route->get('/forgot-password', PasswordController::class . ':createForgotPassword')->setName('forgot.password');
    $route->post('/forgot-password', PasswordController::class . ':forgotPassword');
    $route->get('/reset-password', PasswordController::class.':resetPassword')->setName('reset.password');
    $route->get('/update-password', PasswordController::class.':createUpdatePassword')->setName('update.password');
    $route->post('/update-password', PasswordController::class.':updatePassword');

})->add(new GuestMiddleware($container));



namespace App\Controllers;

class HomeController extends Controller
{
	public function index(Request $request, Response $response)
	{
		return view($response,'index.twig');
	}
}



namespace App\Models;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
	protected $table = 'users';

	protected $fillable = [
		'email',
		'username',
		'password',
	];

}



namespace App\Middleware;

use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\RequestHandlerInterface as RequestHandler;

class AuthMiddleware extends Middleware
{
	public function __invoke(Request $request, RequestHandler $handler)
	{
	   if(! $this->container->get('auth')->isLogin()) {
              return redirect()->route('login')->with('error', 'Access denied, you need to login.');
            }
        $response = $handler->handle($request);
        return $response;
	}
}


namespace App\Controllers\Auth;
use App\Controllers\Controller;
use Respect\Validation\Validator as v;

class AuthController extends Controller
{
	public function register(Request $request, Response $response)
	{
		$validation = $this->validator->validate($request, [
			'email' => v::noWhitespace()->notEmpty()->email(),
                        'username' => v::noWhitespace()->notEmpty()->alnum(),
                        'password' => v::notEmpty()->stringType()->length(8),
		]);

		if ($validation->failed()) {
		    redirect()->route('register');
		}

		//	more coding here
	}
}
shell
|-- slim-born
	|-- app
		|-- Auth
		|-- Controllers
		|-- Middleware
		|-- Models
		|-- Lib
	    |-- bootstrap
		|-- app.php
        |-- database.php
        |-- helper.php
    |-- logs
	|-- public
	|-- resources
    |-- route
	....