blog-app/Routes.php

68 lines
2.8 KiB
PHP
Raw Permalink Normal View History

2021-09-16 20:27:51 +08:00
<?php
$route->filter('auth', function () {
if (! isset($_SESSION['is_auth'])) {
header('Location: /user/login');
return false;
}
});
$route->filter('csrf', function () {
if ($_POST['_token'] != $_SESSION['_token']) {
header('HTTP/1.0 403 Forbidden');
die();
return false;
}
});
$route->get('/404', function () {
return view('404');
});
$route->get('/{page:i}?', ['Controllers\\Post', 'index']);
$route->group(['prefix' => 'user'], function ($route) {
$route->controller('/password', 'Controllers\\Password');
$route->get('/activate', ['Controllers\\User', 'activate']);
$route->get('/email/verify', ['Controllers\\User', 'verifyEmail'], ['before' => 'auth']);
$route->get('/login', ['Controllers\\User', 'showLogin']);
$route->get('/register', ['Controllers\\User', 'showCreate']);
$route->get('/settings', ['Controllers\\User', 'showSettings'], ['before' => 'auth']);
$route->get('/{name:[\w\d._]+}', ['Controllers\\User', 'show']);
$route->get('/{name:[\w\d._]+}/{page:i}?', ['Controllers\\User', 'show']);
$route->group(['before' => 'csrf'], function ($route) {
$route->post('/delete', ['Controllers\\User', 'delete'], ['before' => 'auth']);
$route->post('/login', ['Controllers\\User', 'login']);
$route->post('/logout', ['Controllers\\User', 'logout'], ['before' => 'auth']);
$route->post('/password/create', ['Controllers\\Password', 'create']);
$route->post('/password/reset', ['Controllers\\Password', 'reset']);
$route->post('/register', ['Controllers\\User', 'create']);
$route->post('/settings', ['Controllers\\User', 'update'], ['before' => 'auth']);
});
});
$route->group(['prefix' => 'post'], function ($route) {
$route->get('/create', ['Controllers\\Post', 'showCreate'], ['before' => 'auth']);
$route->get('/{id:i}', ['Controllers\\Post', 'show']);
$route->get('/{id:i}/edit', ['Controllers\\Post', 'showUpdate'], ['before' => 'auth']);
$route->group(['before' => ['auth', 'csrf']], function ($route) {
$route->post('/create', ['Controllers\\Post', 'create']);
$route->post('/{id:i}/delete', ['Controllers\\Post', 'delete']);
$route->post('/{id:i}/edit', ['Controllers\\Post', 'update']);
});
});
$route->group(['prefix' => 'comment', 'before' => ['auth', 'csrf']], function ($route) {
$route->post('/create', ['Controllers\Comment', 'create']);
$route->post('/{id:i}/delete', ['Controllers\Comment', 'delete']);
$route->post('/{id:i}/edit', ['Controllers\Comment', 'update']);
});
$route->get('/tag/{id:i}', ['Controllers\\Tag', 'show']);
$route->get('/tag/{id:i}/{page:i}?', ['Controllers\\Tag', 'show']);
$route->get('/search/{keyword:[^/]+}', ['Controllers\\Search', 'show']);
$route->get('/search/{keyword:[^/]+}/{page:i}?', ['Controllers\\Search', 'show']);