Rewrites and Formatting

This commit is contained in:
Kilian Hofmann 2024-07-22 03:34:56 +02:00
parent 3e22aaf30c
commit 20db2301cb
10 changed files with 28 additions and 56 deletions

View File

@ -1,36 +1,21 @@
RewriteEngine On RewriteEngine On
## RewriteBase /phpCourse/exam/
## You may need to uncomment the following line for some hosting environments,
## if you have installed to a subdirectory, enter the name here also.
##
RewriteBase /phpCourse/exam
##
## Black listed folders
##
RewriteRule ^phpCourse/exam/config/.* index.php [L,NC]
RewriteRule ^phpCourse/exam/vendor/.* index.php [L,NC]
RewriteRule ^phpCourse/exam/routes/.* index.php [L,NC]
RewriteRule ^phpCourse/exam/react/.* index.php [L,NC]
##
## API routes ## API routes
##
RewriteCond %{REQUEST_FILENAME} /api/.* RewriteCond %{REQUEST_FILENAME} /api/.*
RewriteCond %{REQUEST_FILENAME} !/api/docs RewriteCond %{REQUEST_FILENAME} !/api/docs
RewriteRule ^ api/index.php [L,NC,QSA] RewriteRule ^ api/index.php [L,NC,QSA]
## ## Rewrite all routes other than the specified paths
## Standard routes
##
RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !/api/docs RewriteCond %{REQUEST_FILENAME} !/api/docs
RewriteCond %{REQUEST_FILENAME} !/dist RewriteCond %{REQUEST_FILENAME} !/dist
RewriteRule ^ dist [L,NC,QSA] RewriteRule ^ dist [L,NC,QSA]
## Rewrite all files unless in the directories specified
RewriteCond %{REQUEST_FILENAME} -f RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} !/storage/.* RewriteCond %{REQUEST_FILENAME} !/storage/.*
RewriteCond %{REQUEST_FILENAME} !/dist/.* RewriteCond %{REQUEST_FILENAME} !/dist/.*
RewriteCond %{REQUEST_FILENAME} !/api/docs RewriteCond %{REQUEST_FILENAME} !/api/docs
RewriteRule ^ dist [L,NC] RewriteRule ^ dist [L,NC,QSA]

View File

@ -3,7 +3,7 @@
namespace Api\Logout; namespace Api\Logout;
use Khofmann\Api\Api; use Khofmann\Api\Api;
use \Khofmann\Models\User\User; use Khofmann\Models\User\User;
use Khofmann\Request\Request; use Khofmann\Request\Request;
use Khofmann\Response\Response; use Khofmann\Response\Response;

View File

@ -16,8 +16,9 @@ use Pecee\SimpleRouter\Handlers\EventHandler;
use Pecee\SimpleRouter\Event\EventArgument; use Pecee\SimpleRouter\Event\EventArgument;
use Pecee\SimpleRouter\Route\ILoadableRoute; use Pecee\SimpleRouter\Route\ILoadableRoute;
use Pecee\SimpleRouter\Route\IGroupRoute; use Pecee\SimpleRouter\Route\IGroupRoute;
use Config\Config;
// Router base path // Router base path
$basePath = Config\Config::getBasePath() . "api"; $basePath = Config::getBasePath() . "api";
// Router event handler for prepending base path // Router event handler for prepending base path
$eventHandler = new EventHandler(); $eventHandler = new EventHandler();
$eventHandler->register(EventHandler::EVENT_ADD_ROUTE, function (EventArgument $event) use ($basePath) { $eventHandler->register(EventHandler::EVENT_ADD_ROUTE, function (EventArgument $event) use ($basePath) {

View File

@ -1,30 +1,3 @@
RewriteEngine On Order deny,allow
Deny from all
## Allow from 127.0.0.1
## You may need to uncomment the following line for some hosting environments,
## if you have installed to a subdirectory, enter the name here also.
##
RewriteBase /phpCourse/exam
##
## Black listed folders
##
RewriteRule ^phpCourse/exam/config/.* index.php [L,NC]
RewriteRule ^phpCourse/exam/vendor/.* index.php [L,NC]
RewriteRule ^phpCourse/exam/routes/.* index.php [L,NC]
RewriteRule ^phpCourse/exam/react/.* index.php [L,NC]
##
## API routes
##
RewriteCond %{REQUEST_FILENAME} /api/.*
RewriteCond %{REQUEST_FILENAME} !/api/docs
RewriteRule ^ api/index.php [L,NC,QSA]
##
## Standard routes
##
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !/api/docs
RewriteCond %{REQUEST_FILENAME} !/dist
RewriteRule ^ dist [L,NC,QSA]

View File

@ -3,6 +3,7 @@
namespace Khofmann\Database; namespace Khofmann\Database;
use PDO; use PDO;
use Config\Config;
class Database extends PDO class Database extends PDO
{ {
@ -17,7 +18,7 @@ class Database extends PDO
{ {
$cls = static::class; $cls = static::class;
if (!isset(self::$instances[$cls])) { if (!isset(self::$instances[$cls])) {
$dataAccess = \Config\Config::getDatabase(); $dataAccess = Config::getDatabase();
self::$instances[$cls] = new static( self::$instances[$cls] = new static(
"mysql:host={$dataAccess["host"]};dbname={$dataAccess["database"]};charset={$dataAccess["charset"]}", "mysql:host={$dataAccess["host"]};dbname={$dataAccess["database"]};charset={$dataAccess["charset"]}",
$dataAccess["user"], $dataAccess["user"],

View File

@ -14,6 +14,6 @@ class Request
public static function header(string $name, $defaultValue = null, bool $tryParse = true): ?string public static function header(string $name, $defaultValue = null, bool $tryParse = true): ?string
{ {
return request()->getHeader($name, $defaultValue, $tryParse); return Request::request()->getHeader($name, $defaultValue, $tryParse);
} }
} }

View File

@ -2,6 +2,8 @@
namespace Config; namespace Config;
use Exception;
class Config class Config
{ {
private static array $instances = []; private static array $instances = [];
@ -21,7 +23,7 @@ class Config
public function __wakeup() public function __wakeup()
{ {
throw new \Exception("Cannot unserialize a singleton."); throw new Exception("Cannot unserialize a singleton.");
} }
private static function getInstance(): Config private static function getInstance(): Config

7
exam/dist/.htaccess vendored Normal file
View File

@ -0,0 +1,7 @@
RewriteEngine On
RewriteBase /phpCourse/exam/dist/
## Frontend routing required all non files to be rewritten to the entry point
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [L,NC,QSA]

3
exam/react/.htaccess Normal file
View File

@ -0,0 +1,3 @@
Order deny,allow
Deny from all
Allow from 127.0.0.1

View File

@ -22,7 +22,7 @@ SimpleRouter::post("/register", [Api\Register\Register::class, "post"]);
/* /*
* Normal Auth routes * Normal Auth routes
*/ */
SimpleRouter::group(["middleware" => \Khofmann\Auth\Auth::class], function () { SimpleRouter::group(["middleware" => Khofmann\Auth\Auth::class], function () {
// Login // Login
SimpleRouter::post("/logout", [Api\Logout\Logout::class, "post"]); SimpleRouter::post("/logout", [Api\Logout\Logout::class, "post"]);
// Get any user // Get any user
@ -33,7 +33,7 @@ SimpleRouter::group(["middleware" => \Khofmann\Auth\Auth::class], function () {
/* /*
* Admin Auth routes * Admin Auth routes
*/ */
SimpleRouter::group(["middleware" => \Khofmann\Auth\AdminAuth::class], function () { SimpleRouter::group(["middleware" => Khofmann\Auth\AdminAuth::class], function () {
// Update any user // Update any user
SimpleRouter::patch("/user/{id}", [Api\User\User::class, "patch"]); SimpleRouter::patch("/user/{id}", [Api\User\User::class, "patch"]);
// Delete any user // Delete any user