From e43d138dab35825fb9d45831fabffd85dd5bf505 Mon Sep 17 00:00:00 2001 From: Giuseppe Criscione <18699708+giuscris@users.noreply.github.com> Date: Tue, 5 Nov 2024 22:04:24 +0100 Subject: [PATCH] Improve route patterns and order --- formwork/routes.php | 12 +- formwork/src/Router/Router.php | 14 ++- panel/routes.php | 194 ++++++++++++++++----------------- 3 files changed, 113 insertions(+), 107 deletions(-) diff --git a/formwork/routes.php b/formwork/routes.php index 477a2d9a..4d895b32 100644 --- a/formwork/routes.php +++ b/formwork/routes.php @@ -19,7 +19,7 @@ return [ 'methods' => ['GET', 'POST'], ], 'index.pagination' => [ - 'path' => '/page/{paginationPage:num}/', + 'path' => '/page/{paginationPage:number}/', 'action' => 'Formwork\Controllers\PageController@load', ], 'assets' => [ @@ -27,23 +27,23 @@ return [ 'action' => 'Formwork\Controllers\AssetsController@asset', ], 'assets.template' => [ - 'path' => '/site/templates/assets/{file}/', + 'path' => '/site/templates/assets/{file:all}/', 'action' => 'Formwork\Controllers\AssetsController@template', ], 'tag.pagination' => [ - 'path' => '/{page}/tag/{tagName:aln}/page/{paginationPage:num}/', + 'path' => '/{page:all}/tag/{tagName:slug}/page/{paginationPage:number}/', 'action' => 'Formwork\Controllers\PageController@load', ], 'tag' => [ - 'path' => '/{page}/tag/{tagName:aln}/', + 'path' => '/{page:all}/tag/{tagName:slug}/', 'action' => 'Formwork\Controllers\PageController@load', ], 'page.pagination' => [ - 'path' => '/{page}/page/{paginationPage:num}/', + 'path' => '/{page:all}/page/{paginationPage:number}/', 'action' => 'Formwork\Controllers\PageController@load', ], 'page' => [ - 'path' => '/{page}/', + 'path' => '/{page:all}/', 'action' => 'Formwork\Controllers\PageController@load', 'methods' => ['GET', 'POST'], ], diff --git a/formwork/src/Router/Router.php b/formwork/src/Router/Router.php index 034ebf90..b3e71422 100644 --- a/formwork/src/Router/Router.php +++ b/formwork/src/Router/Router.php @@ -43,9 +43,15 @@ class Router * Array containing route patterns shortcuts */ protected const PATTERN_SHORTCUTS = [ - 'num' => '[0-9]+', - 'aln' => '[A-Za-z0-9-]+', - 'all' => '.+', + 'all' => '[^?#]+', + 'any' => '[^/?#]+', + 'slug' => '[A-Za-z0-9]+(?:-[A-Za-z0-9]+)*', + 'alnum' => '[A-Za-z0-9]+', + 'alpha' => '[A-Za-z]+', + 'digits' => '[0-9]+', + 'xdigits' => '[0-9a-f]+', + 'number' => '[1-9][0-9]*', + 'base64' => '[A-Za-z0-9+/]+={0,2}', ]; /** @@ -379,7 +385,7 @@ class Router */ protected function resolvePatternShortcut(?string $pattern): string { - $pattern ??= 'all'; + $pattern ??= 'any'; return self::PATTERN_SHORTCUTS[$pattern] ?? $pattern; } diff --git a/panel/routes.php b/panel/routes.php index b63aa981..dabe1405 100644 --- a/panel/routes.php +++ b/panel/routes.php @@ -34,30 +34,9 @@ return [ 'action' => 'Formwork\Panel\Controllers\AuthenticationController@logout', ], - 'panel.backup.make' => [ - 'path' => '/backup/make/', - 'action' => 'Formwork\Panel\Controllers\BackupController@make', - 'methods' => ['POST'], - 'types' => ['XHR'], - ], - - 'panel.backup.download' => [ - 'path' => '/backup/download/{backup}/', - 'action' => 'Formwork\Panel\Controllers\BackupController@download', - 'methods' => ['GET', 'POST'], - ], - - 'panel.backup.delete' => [ - 'path' => '/backup/delete/{backup}/', - 'action' => 'Formwork\Panel\Controllers\BackupController@delete', - 'methods' => ['POST'], - ], - - 'panel.cache.clear' => [ - 'path' => '/cache/clear/{type}?/', - 'action' => 'Formwork\Panel\Controllers\CacheController@clear', - 'methods' => ['POST'], - 'types' => ['XHR'], + 'panel.assets' => [ + 'path' => '/assets/{type:alpha}/{file:all}/', + 'action' => 'Formwork\Panel\Controllers\AssetsController@asset', ], 'panel.dashboard' => [ @@ -65,48 +44,11 @@ return [ 'action' => 'Formwork\Panel\Controllers\DashboardController@index', ], - 'panel.options' => [ - 'path' => '/options/', - 'action' => 'Formwork\Panel\Controllers\OptionsController@index', - ], - - 'panel.options.system' => [ - 'path' => '/options/system/', - 'action' => 'Formwork\Panel\Controllers\OptionsController@systemOptions', - 'methods' => ['GET', 'POST'], - ], - - 'panel.options.site' => [ - 'path' => '/options/site/', - 'action' => 'Formwork\Panel\Controllers\OptionsController@siteOptions', - 'methods' => ['GET', 'POST'], - ], - 'panel.pages' => [ 'path' => '/pages/', 'action' => 'Formwork\Panel\Controllers\PagesController@index', ], - 'panel.tools' => [ - 'path' => '/tools/', - 'action' => 'Formwork\Panel\Controllers\ToolsController@index', - ], - - 'panel.tools.backups' => [ - 'path' => '/tools/backups/', - 'action' => 'Formwork\Panel\Controllers\ToolsController@backups', - ], - - 'panel.tools.updates' => [ - 'path' => '/tools/updates/', - 'action' => 'Formwork\Panel\Controllers\ToolsController@updates', - ], - - 'panel.tools.info' => [ - 'path' => '/tools/info/', - 'action' => 'Formwork\Panel\Controllers\ToolsController@info', - ], - 'panel.pages.new' => [ 'path' => '/pages/new/', 'action' => 'Formwork\Panel\Controllers\PagesController@create', @@ -114,19 +56,19 @@ return [ ], 'panel.pages.edit' => [ - 'path' => '/pages/{page}/edit/', + 'path' => '/pages/{page:all}/edit/', 'action' => 'Formwork\Panel\Controllers\PagesController@edit', 'methods' => ['GET', 'POST'], ], 'panel.pages.preview' => [ - 'path' => '/pages/{page}/preview/', + 'path' => '/pages/{page:all}/preview/', 'action' => 'Formwork\Panel\Controllers\PagesController@preview', 'methods' => ['POST'], ], 'panel.pages.edit.lang' => [ - 'path' => '/pages/{page}/edit/language/{language}/', + 'path' => '/pages/{page:all}/edit/language/{language:alpha}/', 'action' => 'Formwork\Panel\Controllers\PagesController@edit', 'methods' => ['GET', 'POST'], ], @@ -138,44 +80,44 @@ return [ 'types' => ['XHR'], ], - 'panel.pages.uploadfile' => [ - 'path' => '/pages/{page}/file/upload/', + 'panel.pages.file' => [ + 'path' => '/pages/{page:all}/file/{filename}/', + 'action' => 'Formwork\Panel\Controllers\PagesController@file', + 'methods' => ['GET', 'POST'], + ], + + 'panel.pages.uploadFile' => [ + 'path' => '/pages/{page:all}/file/upload/', 'action' => 'Formwork\Panel\Controllers\PagesController@uploadFile', 'methods' => ['POST'], ], - 'panel.pages.deletefile' => [ - 'path' => '/pages/{page}/file/{filename}/delete/', + 'panel.pages.deleteFile' => [ + 'path' => '/pages/{page:all}/file/{filename}/delete/', 'action' => 'Formwork\Panel\Controllers\PagesController@deleteFile', 'methods' => ['POST'], ], 'panel.pages.renameFile' => [ - 'path' => '/pages/{page}/file/{filename}/rename/', + 'path' => '/pages/{page:all}/file/{filename}/rename/', 'action' => 'Formwork\Panel\Controllers\PagesController@renameFile', 'methods' => ['POST'], ], 'panel.pages.replaceFile' => [ - 'path' => '/pages/{page}/file/{filename}/replace/', + 'path' => '/pages/{page:all}/file/{filename}/replace/', 'action' => 'Formwork\Panel\Controllers\PagesController@replaceFile', 'methods' => ['POST'], ], - 'panel.pages.file' => [ - 'path' => '/pages/{page}/file/{filename}/', - 'action' => 'Formwork\Panel\Controllers\PagesController@file', - 'methods' => ['GET', 'POST'], - ], - 'panel.pages.delete' => [ - 'path' => '/pages/{page}/delete/', + 'path' => '/pages/{page:all}/delete/', 'action' => 'Formwork\Panel\Controllers\PagesController@delete', 'methods' => ['POST'], ], 'panel.pages.delete.lang' => [ - 'path' => '/pages/{page}/delete/language/{language}/', + 'path' => '/pages/{page:all}/delete/language/{language:alpha}/', 'action' => 'Formwork\Panel\Controllers\PagesController@delete', 'methods' => ['POST'], ], @@ -210,18 +152,6 @@ return [ 'methods' => ['POST'], ], - 'panel.users.delete' => [ - 'path' => '/users/{user:[a-z0-9_-]+}/delete/', - 'action' => 'Formwork\Panel\Controllers\UsersController@delete', - 'methods' => ['POST'], - ], - - 'panel.users.deleteImage' => [ - 'path' => '/users/{user:[a-z0-9_-]+}/image/delete/', - 'action' => 'Formwork\Panel\Controllers\UsersController@deleteImage', - 'methods' => ['POST'], - ], - 'panel.users.profile' => [ 'path' => '/users/{user:[a-z0-9_-]+}/profile/', 'action' => 'Formwork\Panel\Controllers\UsersController@profile', @@ -234,19 +164,89 @@ return [ 'methods' => ['GET'], ], + 'panel.users.delete' => [ + 'path' => '/users/{user:[a-z0-9_-]+}/delete/', + 'action' => 'Formwork\Panel\Controllers\UsersController@delete', + 'methods' => ['POST'], + ], + + 'panel.users.deleteImage' => [ + 'path' => '/users/{user:[a-z0-9_-]+}/image/delete/', + 'action' => 'Formwork\Panel\Controllers\UsersController@deleteImage', + 'methods' => ['POST'], + ], + + 'panel.options' => [ + 'path' => '/options/', + 'action' => 'Formwork\Panel\Controllers\OptionsController@index', + ], + + 'panel.options.system' => [ + 'path' => '/options/system/', + 'action' => 'Formwork\Panel\Controllers\OptionsController@systemOptions', + 'methods' => ['GET', 'POST'], + ], + + 'panel.options.site' => [ + 'path' => '/options/site/', + 'action' => 'Formwork\Panel\Controllers\OptionsController@siteOptions', + 'methods' => ['GET', 'POST'], + ], + + 'panel.tools' => [ + 'path' => '/tools/', + 'action' => 'Formwork\Panel\Controllers\ToolsController@index', + ], + + 'panel.tools.backups' => [ + 'path' => '/tools/backups/', + 'action' => 'Formwork\Panel\Controllers\ToolsController@backups', + ], + + 'panel.tools.updates' => [ + 'path' => '/tools/updates/', + 'action' => 'Formwork\Panel\Controllers\ToolsController@updates', + ], + + 'panel.tools.info' => [ + 'path' => '/tools/info/', + 'action' => 'Formwork\Panel\Controllers\ToolsController@info', + ], + + 'panel.backup.make' => [ + 'path' => '/backup/make/', + 'action' => 'Formwork\Panel\Controllers\BackupController@make', + 'methods' => ['POST'], + 'types' => ['XHR'], + ], + + 'panel.backup.download' => [ + 'path' => '/backup/download/{backup:base64}/', + 'action' => 'Formwork\Panel\Controllers\BackupController@download', + 'methods' => ['GET', 'POST'], + ], + + 'panel.backup.delete' => [ + 'path' => '/backup/delete/{backup:base64}/', + 'action' => 'Formwork\Panel\Controllers\BackupController@delete', + 'methods' => ['POST'], + ], + + 'panel.cache.clear' => [ + 'path' => '/cache/clear/{type:alpha}?/', + 'action' => 'Formwork\Panel\Controllers\CacheController@clear', + 'methods' => ['POST'], + 'types' => ['XHR'], + ], + 'panel.register' => [ 'path' => '/register/', 'action' => 'Formwork\Panel\Controllers\RegisterController@register', 'methods' => ['GET', 'POST'], ], - 'panel.assets' => [ - 'path' => '/assets/{type:aln}/{file}/', - 'action' => 'Formwork\Panel\Controllers\AssetsController@asset', - ], - - 'panel.errors.notfound' => [ - 'path' => '/{route}/', + 'panel.errors.notFound' => [ + 'path' => '/{route:all}/', 'action' => 'Formwork\Panel\Controllers\ErrorsController@notFound', ], ],