PHP code example of romagny13 / micro-php

1. Go to this page and download the library: Download romagny13/micro-php 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/ */

    

romagny13 / micro-php example snippets



ion_start();

$settings = [
    'base' => 'http://localhost:8080/',
    'templates' => __DIR__.'/../templates',
    'db' => [
        'driver' => 'mysql',
        'host' => 'localhost',
        'database' => 'blog',
        'username' => 'root',
        'password' => '',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    ]
];
$app = new \MicroPHP\App($settings);
$router = $app->router;

// dependencies


$injector = $app->injector;

// Eloquent
$capsule = new \Illuminate\Database\Capsule\Manager;
$capsule->addConnection($settings['db']);
$capsule->setAsGlobal();
$capsule->bootEloquent();


$injector
    ->register('db', $capsule)
    ->register('auth', \App\Auth\Auth::class)
    ->register('AuthMiddleware', \App\Middleware\AuthMiddleware::class, [$injector])
    ->register('csrf', \MicroPHP\Csrf\Csrf::class)
    ->register('CheckCsrfMiddleware', \App\Middleware\CheckCsrfMiddleware::class, [$injector])
    ->register('CsrfMiddleware', \App\Middleware\CsrfMiddleware::class, [$injector])
    ->register('flash', \MicroPHP\Flash\Flash::class)
    ->register('HomeController', \App\Controllers\HomeController::class, [$injector])
    ->register('PostController', \App\Controllers\PostController::class, [$injector])
    ->register('AuthController', \App\Controllers\AuthController::class, [$injector]);


// add variables to twig
$renderer = $app->renderer;
$renderer->twig->addGlobal('flash', $injector->get('flash'));
$renderer->twig->addGlobal('auth',[
    'isLogged' => $injector->get('auth')->isLogged(),
    'user' => $injector->get('auth')->user()
]);

$renderer->twig->addFunction(new Twig_SimpleFunction('canAddPost',function() use($injector){
    return $injector->get('auth')->canAddPost();
}));

$renderer->twig->addFunction(new Twig_SimpleFunction('canEditPost',function($post) use($injector){
    return $injector->get('auth')->canEditPost($post);
}));




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

$router->group('/auth', function(){
    $this->get('/signup', 'AuthController:getSignup')->setName('auth.signup');
    $this->post('/signup', 'AuthController:postSignup');
    $this->get('/signin', 'AuthController:getSignin')->setName('auth.signin');
    $this->post('/signin', 'AuthController:postSignin');
    $this->get('/signout', 'AuthController:getSignout')->setName('auth.signout');
});

$router->group('/posts', function(){
    $this->get('', 'PostController:index')->setName('posts.index');
    $this->get('/create', 'PostController:getCreate')->setName('posts.create')->add('AuthMiddleware');
    $this->post('/create', 'PostController:postCreate');
    $this->post('/delete', 'PostController:deletePost')->setName('posts.delete');
});

$router->get('.*', function($route){
    $route->router->go('home');
});

$router
    ->add($injector->get('CheckCsrfMiddleware'))
    ->add($injector->get('CsrfMiddleware'));

composer