1. Go to this page and download the library: Download kerwin/simpleframe 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/ */
namespace App\Http\Controller\Manage;
use Twig\Environment;
class UserController
{
/**
* 使用者管理頁面
*
* @return void
*/
public function index(Environment $twig)
{
echo $twig->render('manage/users/index.twig');
}
}
namespace App\Models;
use Kerwin\Core\Model;
class User extends Model
{
}
$primaryKey = 'id';
// Models
User::class => create(User::class)
public function index(User $user)
{
return $user->all();
}
public function show(User $user, $id)
{
return $user->find($id);
}
public function store(User $user)
{
return $user->insert(['name' => 'Jack', 'email' => '[email protected]']);
}
public function update(User $user, $id)
{
return $user->update($id, ['name' => 'Jack', 'email' => '[email protected]']);
}
public function delete(User $user, $id)
{
return $user->delete($id);
}
public function index(User $user)
{
$users = $user->all();
echo $this->twig->render('index.twig', [
'users' => $users
]);
}
$root = "./";
include($root.'config/settings.php');
use Kerwin\Core\Router\RouteCollector;
use function Kerwin\Core\Router\simpleDispatcher;
$container = /
$route->get('/', 'App\Http\Controller\HomeController');
# /simpleframe/auth
$route->addGroup('/auth', function (RouteCollector $route) {
# /simpleframe/auth/login
$route->get('/login', ['App\Http\Controller\Auth\LoginController', 'index']);
$route->post('/login', ['App\Http\Controller\Auth\LoginController', 'login']);
});
});
});
$dispatcher->process($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI'], $container);
# 如果未登入,就導向404頁面
namespace App\Http\Middleware;
use Closure;
use Twig\Environment;
use Kerwin\Core\Request;
use Kerwin\Core\Support\Facades\Session;
use Kerwin\Core\Router\Middleware\Middleware;
class AuthMiddleware implements Middleware
{
private $twig;
public function __construct(Environment $twig) {
$this->twig = $twig;
}
public function __invoke(Request $request, Closure $next, $arg = NULL)
{
if (!Session::get('USER_ID')) {
echo $this->twig->render('_error/404.twig');
return;
}
return $next($request);
}
}
// Middleware
'auth' => function (Environment $twig) {
return new App\Http\Middleware\AuthMiddleware($twig);
},