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
##
## 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
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
##
## Rewrite all routes other than the specified paths
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !/api/docs
RewriteCond %{REQUEST_FILENAME} !/dist
RewriteRule ^ dist [L,NC,QSA]
## Rewrite all files unless in the directories specified
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} !/storage/.*
RewriteCond %{REQUEST_FILENAME} !/dist/.*
RewriteCond %{REQUEST_FILENAME} !/api/docs
RewriteRule ^ dist [L,NC]
RewriteRule ^ dist [L,NC,QSA]

View File

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

View File

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

View File

@ -1,30 +1,3 @@
RewriteEngine On
##
## 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]
Order deny,allow
Deny from all
Allow from 127.0.0.1

View File

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

View File

@ -14,6 +14,6 @@ class Request
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;
use Exception;
class Config
{
private static array $instances = [];
@ -21,7 +23,7 @@ class Config
public function __wakeup()
{
throw new \Exception("Cannot unserialize a singleton.");
throw new Exception("Cannot unserialize a singleton.");
}
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
*/
SimpleRouter::group(["middleware" => \Khofmann\Auth\Auth::class], function () {
SimpleRouter::group(["middleware" => Khofmann\Auth\Auth::class], function () {
// Login
SimpleRouter::post("/logout", [Api\Logout\Logout::class, "post"]);
// Get any user
@ -33,7 +33,7 @@ SimpleRouter::group(["middleware" => \Khofmann\Auth\Auth::class], function () {
/*
* Admin Auth routes
*/
SimpleRouter::group(["middleware" => \Khofmann\Auth\AdminAuth::class], function () {
SimpleRouter::group(["middleware" => Khofmann\Auth\AdminAuth::class], function () {
// Update any user
SimpleRouter::patch("/user/{id}", [Api\User\User::class, "patch"]);
// Delete any user