From 3b2114cd10bf5aa482251c29388320de16cae770 Mon Sep 17 00:00:00 2001 From: flashwave Date: Wed, 9 Mar 2016 19:41:43 +0100 Subject: [PATCH] added route groups low motivation programming --- composer.json | 2 +- libraries/Router.php | 19 ++++++++++++-- public/authenticate.php | 57 +++++++++++++++++------------------------ routes.php | 26 ++++++++++++------- 4 files changed, 59 insertions(+), 45 deletions(-) diff --git a/composer.json b/composer.json index c3797b8..8cb3357 100644 --- a/composer.json +++ b/composer.json @@ -17,6 +17,6 @@ "jbbcode/jbbcode": "*", "corneltek/cliframework": "*", "phroute/phroute": "^2.1", - "illuminate/database": "5.2.7" + "illuminate/database": "5.2.*" } } diff --git a/libraries/Router.php b/libraries/Router.php index 5da6db7..7d28d6a 100644 --- a/libraries/Router.php +++ b/libraries/Router.php @@ -8,8 +8,8 @@ namespace Sakura; use Phroute\Phroute\Dispatcher; -use Phroute\Phroute\Exception\HttpRouteNotFoundException; use Phroute\Phroute\Exception\HttpMethodNotAllowedException; +use Phroute\Phroute\Exception\HttpRouteNotFoundException; use Phroute\Phroute\RouteCollector; /** @@ -53,7 +53,8 @@ class Router 'PATCH', 'DELETE', 'HEAD', - 'ANY' + 'OPTIONS', + 'ANY', ]; /** @@ -130,6 +131,20 @@ class Router return self::$basePath . self::$router->route($name, $args); } + /** + * Create group. + * + * @param array $filters The filters for this group. + * @param \Closure $callback The containers + * + * @return string The generated URI. + */ + public static function group($filters, $callback) + { + // Execute the inner function + self::$router->group($filters, $callback); + } + /** * Handle requests. * diff --git a/public/authenticate.php b/public/authenticate.php index a92b815..514cae4 100644 --- a/public/authenticate.php +++ b/public/authenticate.php @@ -20,11 +20,9 @@ if (isset($_REQUEST['mode'])) { // Compare time and session so we know the link isn't forged if (!isset($_REQUEST['time']) || $_REQUEST['time'] < time() - 1000) { $renderData['page'] = [ - 'redirect' => $urls->format('AUTH_ACTION'), 'message' => 'Timestamps differ too much, refresh the page and try again.', 'success' => 0, - ]; // Prevent @@ -34,11 +32,9 @@ if (isset($_REQUEST['mode'])) { // Match session ids for the same reason if (!isset($_REQUEST['session']) || $_REQUEST['session'] != session_id()) { $renderData['page'] = [ - 'redirect' => $urls->format('AUTH_ACTION'), 'message' => 'Invalid session, please try again.', 'success' => 0, - ]; // Prevent @@ -62,15 +58,6 @@ if (isset($_REQUEST['mode'])) { if ($continue) { switch ($_REQUEST['mode']) { - case 'logout': - // Add page specific things - $renderData['page'] = [ - 'redirect' => Router::route('main.index'), - 'message' => 'Wrong logout page.', - 'success' => 0, - ]; - break; - case 'changepassword': // Attempt change $passforget = Users::resetPassword( @@ -146,26 +133,6 @@ if (isset($_REQUEST['mode'])) { ]; break; - // Login processing - case 'login': - // Add page specific things - $renderData['page'] = [ - 'redirect' => Router::route('auth.login'), - 'message' => 'Wrong login page.', - 'success' => 0, - ]; - break; - - // Registration processing - case 'register': - // Add page specific things - $renderData['page'] = [ - 'redirect' => Router::route('auth.register'), - 'message' => 'Wrong registration page.', - 'success' => 0, - ]; - break; - // Unforgetting passwords case 'forgotpassword': // Attempt send @@ -187,6 +154,30 @@ if (isset($_REQUEST['mode'])) { ]; break; + case 'logout': + $renderData['page'] = [ + 'redirect' => Router::route('main.index'), + 'message' => 'Wrong logout page.', + 'success' => 0, + ]; + break; + + case 'login': + $renderData['page'] = [ + 'redirect' => Router::route('auth.login'), + 'message' => 'Wrong login page.', + 'success' => 0, + ]; + break; + + case 'register': + $renderData['page'] = [ + 'redirect' => Router::route('auth.register'), + 'message' => 'Wrong registration page.', + 'success' => 0, + ]; + break; + } } diff --git a/routes.php b/routes.php index b52014d..568c8d6 100644 --- a/routes.php +++ b/routes.php @@ -25,17 +25,23 @@ Router::post('/reactivate', 'AuthController@reactivatePost', 'auth.reactivate'); Router::get('/activate', 'AuthController@activate', 'auth.activate'); // News -Router::get('/news', 'MetaController@news', 'news.index'); -Router::get('/news/{category}', 'MetaController@news', 'news.category'); -Router::get('/news/{category}/{id}', 'MetaController@news', 'news.post'); +Router::group(['prefix' => 'news'], function () { + Router::get('/', 'MetaController@news', 'news.index'); + Router::get('/{category}', 'MetaController@news', 'news.category'); + Router::get('/{category}/{id}', 'MetaController@news', 'news.post'); +}); // Forum -Router::get('/forum', 'ForumController@index', 'forums.index'); -Router::get('/forum/{id}', 'ForumController@forum', 'forums.forum'); +Router::group(['prefix' => 'forum'], function () { + Router::get('/', 'ForumController@index', 'forums.index'); + Router::get('/{id}', 'ForumController@forum', 'forums.forum'); +}); // Members -Router::get('/members', 'UserController@members', 'members.index'); -Router::get('/members/{rank}', 'UserController@members', 'members.rank'); +Router::group(['prefix' => 'members'], function () { + Router::get('/', 'UserController@members', 'members.index'); + Router::get('/{rank}', 'UserController@members', 'members.rank'); +}); // User Router::get('/u/{id}', 'UserController@profile', 'user.profile'); @@ -46,8 +52,10 @@ Router::get('/a/{id}', 'FileController@avatar', 'file.avatar'); Router::get('/bg/{id}', 'FileController@background', 'file.background'); // Premium -Router::get('/support', 'PremiumController@index', 'premium.index'); -Router::get('/support/tracker', 'PremiumController@tracker', 'premium.tracker'); +Router::group(['prefix' => 'support'], function () { + Router::get('/', 'PremiumController@index', 'premium.index'); + Router::get('/tracker', 'PremiumController@tracker', 'premium.tracker'); +}); // Management /*