Skip to content

Commit

Permalink
Add route rewrites
Browse files Browse the repository at this point in the history
  • Loading branch information
nox7 committed Aug 14, 2021
1 parent e607393 commit 0c12898
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/Http/Rewrite.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Nox\Http;

/**
* An abstraction to handle responses back to the client/router system.
*/
class Rewrite{

public string $path;
public int $statusCode;

public function __construct(string $path, int $statusCode = 200){
$this->path = $path;
$this->statusCode = $statusCode;
}

}
23 changes: 22 additions & 1 deletion src/Router/RequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Nox\Router;

use Nox\Http\Redirect;
use Nox\Http\Rewrite;
use Nox\Router\Exceptions\RecursionDepthExceeded;

require_once __DIR__ . "/ViewSettings.php";
Expand Down Expand Up @@ -59,11 +60,31 @@ public function getRouteResult(): mixed{
*/
public function processRequest(): void{
$routeResult = $this->getRouteResult();
if ($routeResult instanceof Redirect){
if ($routeResult instanceof Redirect) {
/** @var Redirect $routeResult */
http_response_code($routeResult->statusCode);
header(
sprintf("location: %s", $routeResult->path)
);
exit();
}elseif ($routeResult instanceof Rewrite){
/** @var Rewrite $routeResult */
http_response_code($routeResult->statusCode);
$rewriteRouter = new Router(
requestPath:"/404",
requestMethod: "get",
);
$rewriteRouter->staticFileHandler = $this->router->staticFileHandler;
$rewriteRouter->viewSettings = $this->router->viewSettings;
$rewriteRouter->noxConfig = $this->router->noxConfig;
$rewriteRouter->controllersFolder = $this->router->controllersFolder;
$rewriteRouter->loadMVCControllers();
$rewriteRouter = new RequestHandler(
$rewriteRouter,
++$this->recursionDepth,
);
$rewriteRouter->processRequest();

exit();
}elseif ($routeResult !== null){
// Successful route with an outputtable result.
Expand Down

0 comments on commit 0c12898

Please sign in to comment.