added helpers to req/resp and cleaned up phpcs/stan errors

pull/545/head
n0nag0n 11 months ago
parent a60a31c4bd
commit 4d4c0d5420

@ -66,15 +66,14 @@ use flight\net\Route;
*/ */
class Engine class Engine
{ {
/**
/** * @var array<string> List of methods that can be extended in the Engine class.
* @var array<string> List of methods that can be extended in the Engine class. */
*/ private const MAPPABLE_METHODS = [
private const MAPPABLE_METHODS = [ 'start', 'stop', 'route', 'halt', 'error', 'notFound',
'start', 'stop', 'route', 'halt', 'error', 'notFound', 'render', 'redirect', 'etag', 'lastModified', 'json', 'jsonp',
'render', 'redirect', 'etag', 'lastModified', 'json', 'jsonp', 'post', 'put', 'patch', 'delete', 'group', 'getUrl'
'post', 'put', 'patch', 'delete', 'group', 'getUrl' ];
];
/** @var array<string, mixed> Stored variables. */ /** @var array<string, mixed> Stored variables. */
protected array $vars = []; protected array $vars = [];

@ -331,6 +331,49 @@ class Request
return $headers; return $headers;
} }
/**
* Alias of Request->getHeader(). Gets a single header.
*
* @param string $header Header name. Can be caps, lowercase, or mixed.
* @param string $default Default value if the header does not exist
*
* @return string
*/
public static function header(string $header, $default = '')
{
return self::getHeader($header, $default);
}
/**
* Alias of Request->getHeaders(). Gets all the request headers
*
* @return array<string, string|int>
*/
public static function headers(): array
{
return self::getHeaders();
}
/**
* Gets the full request URL.
*
* @return string URL
*/
public function getFullUrl(): string
{
return $this->scheme . '://' . $this->host . $this->url;
}
/**
* Grabs the scheme and host. Does not end with a /
*
* @return string
*/
public function getBaseUrl(): string
{
return $this->scheme . '://' . $this->host;
}
/** /**
* Parse query parameters from a URL. * Parse query parameters from a URL.
* *

@ -173,6 +173,34 @@ class Response
return $this; return $this;
} }
/**
* Gets a single header from the response.
*
* @param string $name the name of the header
*
* @return string|null
*/
public function getHeader(string $name): ?string
{
$headers = $this->headers;
// lowercase all the header keys
$headers = array_change_key_case($headers, CASE_LOWER);
return $headers[strtolower($name)] ?? null;
}
/**
* Alias of Response->header(). Adds a header to the response.
*
* @param array<string, int|string>|string $name Header name or array of names and values
* @param ?string $value Header value
*
* @return $this
*/
public function setHeader($name, ?string $value): self
{
return $this->header($name, $value);
}
/** /**
* Returns the headers from the response. * Returns the headers from the response.
* *
@ -183,6 +211,16 @@ class Response
return $this->headers; return $this->headers;
} }
/**
* Alias for Response->headers(). Returns the headers from the response.
*
* @return array<string, int|string|array<int, string>>
*/
public function getHeaders(): array
{
return $this->headers();
}
/** /**
* Writes content to the response body. * Writes content to the response body.
* *

@ -45,7 +45,7 @@ class DocExamplesTest extends TestCase
$this->assertEquals('[]', Flight::response()->getBody()); $this->assertEquals('[]', Flight::response()->getBody());
} }
public function testMapNotFoundMethodV2OutputBuffering() public function testMapNotFoundMethodV2OutputBuffering()
{ {
Flight::map('notFound', function () { Flight::map('notFound', function () {
Flight::json([], 404); Flight::json([], 404);
@ -57,9 +57,9 @@ class DocExamplesTest extends TestCase
echo 'hello world!'; echo 'hello world!';
}); });
Flight::set('flight.v2.output_buffering', true); Flight::set('flight.v2.output_buffering', true);
Flight::start(); Flight::start();
ob_get_clean(); ob_get_clean();
$this->assertEquals(404, Flight::response()->status()); $this->assertEquals(404, Flight::response()->status());
$this->assertEquals('[]', Flight::response()->getBody()); $this->assertEquals('[]', Flight::response()->getBody());
} }

@ -243,37 +243,39 @@ class FlightTest extends TestCase
$this->assertEquals('/user/all_users/check_user/check_one/normalpath', $url); $this->assertEquals('/user/all_users/check_user/check_one/normalpath', $url);
} }
public function testHookOutputBuffering() { public function testHookOutputBuffering()
Flight::route('/test', function () { {
echo 'test'; Flight::route('/test', function () {
}); echo 'test';
});
Flight::before('start', function ($output) {
echo 'hooked before start'; Flight::before('start', function ($output) {
}); echo 'hooked before start';
});
Flight::request()->url = '/test';
Flight::request()->url = '/test';
$this->expectOutputString('hooked before starttest');
Flight::start(); $this->expectOutputString('hooked before starttest');
$this->assertEquals('test', Flight::response()->getBody()); Flight::start();
} $this->assertEquals('test', Flight::response()->getBody());
}
public function testHookOutputBufferingV2OutputBuffering() {
Flight::route('/test', function () { public function testHookOutputBufferingV2OutputBuffering()
echo 'test'; {
}); Flight::route('/test', function () {
echo 'test';
Flight::before('start', function ($output) { });
echo 'hooked before start';
}); Flight::before('start', function ($output) {
echo 'hooked before start';
Flight::set('flight.v2.output_buffering', true); });
Flight::request()->url = '/test';
Flight::set('flight.v2.output_buffering', true);
$this->expectOutputString('hooked before starttest'); Flight::request()->url = '/test';
ob_start();
Flight::start(); $this->expectOutputString('hooked before starttest');
$this->assertEquals('hooked before starttest', Flight::response()->getBody()); ob_start();
} Flight::start();
$this->assertEquals('hooked before starttest', Flight::response()->getBody());
}
} }

@ -205,10 +205,10 @@ class RequestTest extends TestCase
// or the headers that are already in $_SERVER // or the headers that are already in $_SERVER
$this->assertEquals('XMLHttpRequest', $request->getHeader('X-REqUesTed-WiTH')); $this->assertEquals('XMLHttpRequest', $request->getHeader('X-REqUesTed-WiTH'));
$this->assertEquals('32.32.32.32', $request->getHeader('X-Forwarded-For')); $this->assertEquals('32.32.32.32', $request->header('X-Forwarded-For'));
// default values // default values
$this->assertEquals('default value', $request->getHeader('X-Non-Existent-Header', 'default value')); $this->assertEquals('default value', $request->header('X-Non-Existent-Header', 'default value'));
} }
public function testGetHeaders() public function testGetHeaders()
@ -231,7 +231,7 @@ class RequestTest extends TestCase
$_SERVER = []; $_SERVER = [];
$_SERVER['HTTP_X_CUSTOM_HEADER'] = ''; $_SERVER['HTTP_X_CUSTOM_HEADER'] = '';
$request = new Request(); $request = new Request();
$this->assertEquals(['X-Custom-Header' => ''], $request->getHeaders()); $this->assertEquals(['X-Custom-Header' => ''], $request->headers());
} }
public function testGetHeadersWithMultipleHeaders() public function testGetHeadersWithMultipleHeaders()
@ -245,4 +245,38 @@ class RequestTest extends TestCase
'X-Custom-Header2' => 'custom header value 2' 'X-Custom-Header2' => 'custom header value 2'
], $request->getHeaders()); ], $request->getHeaders());
} }
public function testGetFullUrlNoHttps()
{
$_SERVER['HTTP_HOST'] = 'example.com';
$_SERVER['REQUEST_URI'] = '/page?id=1';
$request = new Request();
$this->assertEquals('http://example.com/page?id=1', $request->getFullUrl());
}
public function testGetFullUrlWithHttps()
{
$_SERVER['HTTP_HOST'] = 'localhost:8000';
$_SERVER['REQUEST_URI'] = '/page?id=1';
$_SERVER['HTTPS'] = 'on';
$request = new Request();
$this->assertEquals('https://localhost:8000/page?id=1', $request->getFullUrl());
}
public function testGetBaseUrlNoHttps()
{
$_SERVER['HTTP_HOST'] = 'example.com';
$_SERVER['REQUEST_URI'] = '/page?id=1';
$request = new Request();
$this->assertEquals('http://example.com', $request->getBaseUrl());
}
public function testGetBaseUrlWithHttps()
{
$_SERVER['HTTP_HOST'] = 'localhost:8000';
$_SERVER['REQUEST_URI'] = '/page?id=1';
$_SERVER['HTTPS'] = 'on';
$request = new Request();
$this->assertEquals('https://localhost:8000', $request->getBaseUrl());
}
} }

@ -65,7 +65,7 @@ class ResponseTest extends TestCase
$response = new Response(); $response = new Response();
$response->header('content-type', 'text/html'); $response->header('content-type', 'text/html');
$response->header('x-test', 'test'); $response->header('x-test', 'test');
$this->assertEquals(['content-type' => 'text/html', 'x-test' => 'test'], $response->headers()); $this->assertEquals(['content-type' => 'text/html', 'x-test' => 'test'], $response->getHeaders());
} }
public function testHeaderArray() public function testHeaderArray()
@ -81,6 +81,13 @@ class ResponseTest extends TestCase
$this->assertEquals($response, $response->header('Content-Type', 'text/html')); $this->assertEquals($response, $response->header('Content-Type', 'text/html'));
} }
public function testGetHeaderCrazyCase()
{
$response = new Response();
$response->setHeader('CoNtEnT-tYpE', 'text/html');
$this->assertEquals('text/html', $response->getHeader('content-type'));
}
public function testWrite() public function testWrite()
{ {
$response = new Response(); $response = new Response();
@ -104,6 +111,9 @@ class ResponseTest extends TestCase
public function testClear() public function testClear()
{ {
$response = new Response(); $response = new Response();
// Should clear this echo out
echo 'hi';
$response->write('test'); $response->write('test');
$response->status(404); $response->status(404);
$response->header('Content-Type', 'text/html'); $response->header('Content-Type', 'text/html');
@ -111,6 +121,7 @@ class ResponseTest extends TestCase
$this->assertEquals('', $response->getBody()); $this->assertEquals('', $response->getBody());
$this->assertEquals(200, $response->status()); $this->assertEquals(200, $response->status());
$this->assertEquals([], $response->headers()); $this->assertEquals([], $response->headers());
$this->assertEquals(0, ob_get_length());
} }
public function testCacheSimple() public function testCacheSimple()
@ -219,8 +230,8 @@ class ResponseTest extends TestCase
return $this; return $this;
} }
}; };
$response->header('Content-Type', 'text/html'); $response->setHeader('Content-Type', 'text/html');
$response->header('X-Test', 'test'); $response->setHeader('X-Test', 'test');
$response->write('Something'); $response->write('Something');
$this->expectOutputString('Something'); $this->expectOutputString('Something');

@ -1,9 +1,11 @@
<?php <?php
declare(strict_types=1);
/* /*
* This is the test file where we can open up a quick test server and make * This is the test file where we can open up a quick test server and make
* sure that the UI is really working the way we would expect it to. * sure that the UI is really working the way we would expect it to.
* *
* @author Kristaps Muižnieks https://github.com/krmu * @author Kristaps Muižnieks https://github.com/krmu
*/ */
@ -15,48 +17,48 @@ Flight::set('flight.views.path', './');
Flight::set('flight.v2.output_buffering', true); Flight::set('flight.v2.output_buffering', true);
// Test 1: Root route // Test 1: Root route
Flight::route('/', function(){ Flight::route('/', function () {
echo '<span id="infotext">Route text:</span> Root route works!'; echo '<span id="infotext">Route text:</span> Root route works!';
}); });
Flight::route('/querytestpath', function(){ Flight::route('/querytestpath', function () {
echo '<span id="infotext">Route text:</span> This ir query route<br>'; echo '<span id="infotext">Route text:</span> This ir query route<br>';
echo "I got such query parameters:<pre>"; echo "I got such query parameters:<pre>";
print_r(Flight::request()->query); print_r(Flight::request()->query);
echo "</pre>"; echo "</pre>";
},false,"querytestpath"); }, false, "querytestpath");
// Test 2: Simple route // Test 2: Simple route
Flight::route('/test', function(){ Flight::route('/test', function () {
echo '<span id="infotext">Route text:</span> Test route works!'; echo '<span id="infotext">Route text:</span> Test route works!';
}); });
// Test 3: Route with parameter // Test 3: Route with parameter
Flight::route('/user/@name', function($name){ Flight::route('/user/@name', function ($name) {
echo "<span id='infotext'>Route text:</span> Hello, $name!"; echo "<span id='infotext'>Route text:</span> Hello, $name!";
}); });
Flight::route('POST /postpage', function () { Flight::route('POST /postpage', function () {
echo '<span id="infotext">Route text:</span> THIS IS POST METHOD PAGE'; echo '<span id="infotext">Route text:</span> THIS IS POST METHOD PAGE';
},false,"postpage"); }, false, "postpage");
// Test 4: Grouped routes // Test 4: Grouped routes
Flight::group('/group', function(){ Flight::group('/group', function () {
Flight::route('/test', function(){ Flight::route('/test', function () {
echo '<span id="infotext">Route text:</span> Group test route works!'; echo '<span id="infotext">Route text:</span> Group test route works!';
}); });
Flight::route('/user/@name', function($name){ Flight::route('/user/@name', function ($name) {
echo "<span id='infotext'>Route text:</span> There is variable called name and it is $name"; echo "<span id='infotext'>Route text:</span> There is variable called name and it is $name";
}); });
Flight::group('/group1', function(){ Flight::group('/group1', function () {
Flight::group('/group2', function(){ Flight::group('/group2', function () {
Flight::group('/group3', function(){ Flight::group('/group3', function () {
Flight::group('/group4', function(){ Flight::group('/group4', function () {
Flight::group('/group5', function(){ Flight::group('/group5', function () {
Flight::group('/group6', function(){ Flight::group('/group6', function () {
Flight::group('/group7', function(){ Flight::group('/group7', function () {
Flight::group('/group8', function(){ Flight::group('/group8', function () {
Flight::route('/final_group', function(){ Flight::route('/final_group', function () {
echo 'Mega Group test route works!'; echo 'Mega Group test route works!';
},false,"final_group"); }, false, "final_group");
}); });
}); });
}); });
@ -64,39 +66,41 @@ Flight::group('/group', function(){
}); });
}); });
}); });
}); });
}); });
// Test 5: Route alias // Test 5: Route alias
Flight::route('/alias', function(){ Flight::route('/alias', function () {
echo '<span id="infotext">Route text:</span> Alias route works!'; echo '<span id="infotext">Route text:</span> Alias route works!';
}, false, 'aliasroute'); }, false, 'aliasroute');
class authCheck { class AuthCheck
public function before(){ {
if(!isset($_COOKIE['user'])){ public function before()
{
if (!isset($_COOKIE['user'])) {
echo '<span id="infotext">Middleware text:</span> You are not authorized to access this route!'; echo '<span id="infotext">Middleware text:</span> You are not authorized to access this route!';
} }
} }
} }
$middle = new authCheck(); $middle = new AuthCheck();
// Test 6: Route with middleware // Test 6: Route with middleware
Flight::route('/protected', function(){ Flight::route('/protected', function () {
echo '<span id="infotext">Route text:</span> Protected route works!'; echo '<span id="infotext">Route text:</span> Protected route works!';
})->addMiddleware([$middle]); })->addMiddleware([$middle]);
// Test 7: Route with template // Test 7: Route with template
Flight::route('/template/@name', function($name){ Flight::route('/template/@name', function ($name) {
Flight::render('template.php', ['name' => $name]); Flight::render('template.phtml', ['name' => $name]);
}); });
Flight::set('flight.views.path', './'); Flight::set('flight.views.path', './');
Flight::map('error', function (Throwable $error) { Flight::map('error', function (Throwable $error) {
echo "<h1> An error occurred, mapped error method worked, error bellow </h1>"; echo "<h1> An error occurred, mapped error method worked, error bellow </h1>";
echo '<pre style="border: 2px solid red; padding: 21px; background: lightgray; font-weight: bold;">'; echo '<pre style="border: 2px solid red; padding: 21px; background: lightgray; font-weight: bold;">';
echo str_replace(getenv('PWD'),"***CLASSIFIED*****",$error->getTraceAsString()); echo str_replace(getenv('PWD'), "***CLASSIFIED*****", $error->getTraceAsString());
echo "</pre>"; echo "</pre>";
echo "<a href='/'>Go back</a>"; echo "<a href='/'>Go back</a>";
}); });
Flight::map('notFound', function() { Flight::map('notFound', function () {
echo '<span id="infotext">Route text:</span> The requested URL was not found'; echo '<span id="infotext">Route text:</span> The requested URL was not found';
echo "<a href='/'>Go back</a>"; echo "<a href='/'>Go back</a>";
}); });
@ -158,7 +162,7 @@ echo '
<li><a href="/template/templatevariable">Template path</a></li> <li><a href="/template/templatevariable">Template path</a></li>
<li><a href="/querytestpath?test=1&variable2=uuid&variable3=tester">Query path</a></li> <li><a href="/querytestpath?test=1&variable2=uuid&variable3=tester">Query path</a></li>
<li><a href="/postpage">Post method test page - should be 404</a></li> <li><a href="/postpage">Post method test page - should be 404</a></li>
<li><a href="'.Flight::getUrl('final_group').'">Mega group</a></li> <li><a href="' . Flight::getUrl('final_group') . '">Mega group</a></li>
</ul>'; </ul>';
Flight::before('start', function ($params) { Flight::before('start', function ($params) {
echo '<div id="container">'; echo '<div id="container">';
@ -172,5 +176,3 @@ Flight::after('start', function ($params) {
echo "</div>"; echo "</div>";
}); });
Flight::start(); Flight::start();

@ -1 +1 @@
<span id="infotext">Route text:</span> Template <?=$name?> works! <span id="infotext">Route text:</span> Template <?=$name?> works!

@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
class AuthCheck
{
/**
* Before
*
* @return void
*/
public function before()
{
if (!isset($_COOKIE['user'])) {
echo '<span id="infotext">Middleware text:</span> You are not authorized to access this route!';
}
}
}

@ -0,0 +1,93 @@
<?php
declare(strict_types=1);
class LayoutMiddleware
{
/**
* Before
*
* @return void
*/
public function before()
{
$final_route = Flight::getUrl('final_group');
echo <<<HTML
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}
li {
float: left;
}
#infotext {
font-weight: bold;
color: blueviolet;
}
li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover {
background-color: #111;
}
#container {
color: #333;
font-size: 16px;
line-height: 1.5;
margin: 20px 0;
padding: 10px;
border: 1px solid #ddd;
background-color: #f9f9f9;
}
#debugrequest {
color: #333;
font-size: 16px;
line-height: 1.5;
margin: 20px 0;
padding: 10px;
border: 1px solid #ddd;
background-color: #f9f9f9;
}
</style>
<ul>
<li><a href="/">Root Route</a></li>
<li><a href="/test">Test Route</a></li>
<li><a href="/user/John">User Route with Parameter (John)</a></li>
<li><a href="/group/test">Grouped Test Route</a></li>
<li><a href="/group/user/Jane">Grouped User Route with Parameter (Jane)</a></li>
<li><a href="/alias">Alias Route</a></li>
<li><a href="/protected">Protected path</a></li>
<li><a href="/template/templatevariable">Template path</a></li>
<li><a href="/querytestpath?test=1&variable2=uuid&variable3=tester">Query path</a></li>
<li><a href="/postpage">404 Not Found</a></li>
<li><a href="{$final_route}">Mega group</a></li>
<li><a href="/error">Error</a></li>
</ul>
HTML;
echo '<div id="container">';
}
public function after()
{
echo '</div>';
echo '<div id="debugrequest">';
echo "<h2>Request Information</h2><pre>";
print_r(Flight::request());
echo '<h3>Raw Request Information</h3>';
print_r($_SERVER);
echo "</pre><h2>Response Information</h2><pre>";
print_r(Flight::response());
echo "</pre>";
echo "</div>";
}
}

@ -1,199 +1,115 @@
<?php <?php
declare(strict_types=1);
/* /*
* This is the test file where we can open up a quick test server and make * This is the test file where we can open up a quick test server and make
* sure that the UI is really working the way we would expect it to. * sure that the UI is really working the way we would expect it to.
* *
* @author Kristaps Muižnieks https://github.com/krmu * @author Kristaps Muižnieks https://github.com/krmu
*/ */
require file_exists(__DIR__ . '/../../vendor/autoload.php') ? __DIR__ . '/../../vendor/autoload.php' : __DIR__ . '/../../flight/autoload.php'; require file_exists(__DIR__ . '/../../vendor/autoload.php') ? __DIR__ . '/../../vendor/autoload.php' : __DIR__ . '/../../flight/autoload.php';
Flight::set('flight.content_length', false); Flight::set('flight.content_length', false);
Flight::set('flight.views.path', './'); Flight::set('flight.views.path', './');
//Flight::set('flight.v2.output_buffering', true); //Flight::set('flight.v2.output_buffering', true);
class LayoutMiddleware { require_once 'LayoutMiddleware.php';
public function before() {
$final_route = Flight::getUrl('final_group'); Flight::group('', function () {
echo <<<HTML
<style> // Test 1: Root route
ul { Flight::route('/', function () {
list-style-type: none; echo '<span id="infotext">Route text:</span> Root route works!';
margin: 0; });
padding: 0; Flight::route('/querytestpath', function () {
overflow: hidden; echo '<span id="infotext">Route text:</span> This ir query route<br>';
background-color: #333; echo "I got such query parameters:<pre>";
} print_r(Flight::request()->query);
echo "</pre>";
li { }, false, "querytestpath");
float: left;
} // Test 2: Simple route
#infotext { Flight::route('/test', function () {
font-weight: bold; echo '<span id="infotext">Route text:</span> Test route works!';
color: blueviolet; });
}
li a { // Test 3: Route with parameter
display: block; Flight::route('/user/@name', function ($name) {
color: white; echo "<span id='infotext'>Route text:</span> Hello, $name!";
text-align: center; });
padding: 14px 16px; Flight::route('POST /postpage', function () {
text-decoration: none; echo '<span id="infotext">Route text:</span> THIS IS POST METHOD PAGE';
} }, false, "postpage");
li a:hover { // Test 4: Grouped routes
background-color: #111; Flight::group('/group', function () {
} Flight::route('/test', function () {
#container { echo '<span id="infotext">Route text:</span> Group test route works!';
color: #333; });
font-size: 16px; Flight::route('/user/@name', function ($name) {
line-height: 1.5; echo "<span id='infotext'>Route text:</span> There is variable called name and it is $name";
margin: 20px 0; });
padding: 10px; Flight::group('/group1', function () {
border: 1px solid #ddd; Flight::group('/group2', function () {
background-color: #f9f9f9; Flight::group('/group3', function () {
} Flight::group('/group4', function () {
#debugrequest { Flight::group('/group5', function () {
color: #333; Flight::group('/group6', function () {
font-size: 16px; Flight::group('/group7', function () {
line-height: 1.5; Flight::group('/group8', function () {
margin: 20px 0; Flight::route('/final_group', function () {
padding: 10px; echo 'Mega Group test route works!';
border: 1px solid #ddd; }, false, "final_group");
background-color: #f9f9f9; });
} });
</style> });
<ul> });
<li><a href="/">Root Route</a></li> });
<li><a href="/test">Test Route</a></li> });
<li><a href="/user/John">User Route with Parameter (John)</a></li> });
<li><a href="/group/test">Grouped Test Route</a></li> });
<li><a href="/group/user/Jane">Grouped User Route with Parameter (Jane)</a></li> });
<li><a href="/alias">Alias Route</a></li>
<li><a href="/protected">Protected path</a></li> // Test 5: Route alias
<li><a href="/template/templatevariable">Template path</a></li> Flight::route('/alias', function () {
<li><a href="/querytestpath?test=1&variable2=uuid&variable3=tester">Query path</a></li> echo '<span id="infotext">Route text:</span> Alias route works!';
<li><a href="/postpage">404 Not Found</a></li> }, false, 'aliasroute');
<li><a href="{$final_route}">Mega group</a></li>
<li><a href="/error">Error</a></li> /** Middleware test */
</ul> include_once 'AuthCheck.php';
HTML; $middle = new AuthCheck();
echo '<div id="container">'; // Test 6: Route with middleware
} Flight::route('/protected', function () {
echo '<span id="infotext">Route text:</span> Protected route works!';
public function after() { })->addMiddleware([$middle]);
echo '</div>';
echo '<div id="debugrequest">'; // Test 7: Route with template
echo "<h2>Request Information</h2><pre>"; Flight::route('/template/@name', function ($name) {
print_r(Flight::request()); Flight::render('template.phtml', ['name' => $name]);
echo '<h3>Raw Request Information</h3>'; });
print_r($_SERVER);
echo "</pre><h2>Response Information</h2><pre>"; // Test 8: Throw an error
print_r(Flight::response()); Flight::route('/error', function () {
echo "</pre>"; trigger_error('This is a successful error');
echo "</div>"; });
} }, [ new LayoutMiddleware() ]);
}
Flight::group('', function() {
// Test 1: Root route
Flight::route('/', function(){
echo '<span id="infotext">Route text:</span> Root route works!';
});
Flight::route('/querytestpath', function(){
echo '<span id="infotext">Route text:</span> This ir query route<br>';
echo "I got such query parameters:<pre>";
print_r(Flight::request()->query);
echo "</pre>";
},false,"querytestpath");
// Test 2: Simple route
Flight::route('/test', function(){
echo '<span id="infotext">Route text:</span> Test route works!';
});
// Test 3: Route with parameter
Flight::route('/user/@name', function($name){
echo "<span id='infotext'>Route text:</span> Hello, $name!";
});
Flight::route('POST /postpage', function () {
echo '<span id="infotext">Route text:</span> THIS IS POST METHOD PAGE';
},false,"postpage");
// Test 4: Grouped routes
Flight::group('/group', function(){
Flight::route('/test', function(){
echo '<span id="infotext">Route text:</span> Group test route works!';
});
Flight::route('/user/@name', function($name){
echo "<span id='infotext'>Route text:</span> There is variable called name and it is $name";
});
Flight::group('/group1', function(){
Flight::group('/group2', function(){
Flight::group('/group3', function(){
Flight::group('/group4', function(){
Flight::group('/group5', function(){
Flight::group('/group6', function(){
Flight::group('/group7', function(){
Flight::group('/group8', function(){
Flight::route('/final_group', function(){
echo 'Mega Group test route works!';
},false,"final_group");
});
});
});
});
});
});
});
});
});
// Test 5: Route alias
Flight::route('/alias', function(){
echo '<span id="infotext">Route text:</span> Alias route works!';
}, false, 'aliasroute');
/** middleware test */
class authCheck {
public function before(){
if(!isset($_COOKIE['user'])){
echo '<span id="infotext">Middleware text:</span> You are not authorized to access this route!';
}
}
}
$middle = new authCheck();
// Test 6: Route with middleware
Flight::route('/protected', function(){
echo '<span id="infotext">Route text:</span> Protected route works!';
})->addMiddleware([$middle]);
// Test 7: Route with template
Flight::route('/template/@name', function($name){
Flight::render('template.php', ['name' => $name]);
});
// Test 8: Throw an error
Flight::route('/error', function() {
trigger_error('This is a successful error');
});
}, [ new LayoutMiddleware ]);
Flight::map('error', function (Throwable $e) { Flight::map('error', function (Throwable $e) {
echo sprintf( echo sprintf(
'<h1>500 Internal Server Error</h1>' . '<h1>500 Internal Server Error</h1>' .
'<h3>%s (%s)</h3>' . '<h3>%s (%s)</h3>' .
'<pre style="border: 2px solid red; padding: 21px; background: lightgray; font-weight: bold;">%s</pre>', '<pre style="border: 2px solid red; padding: 21px; background: lightgray; font-weight: bold;">%s</pre>',
$e->getMessage(), $e->getMessage(),
$e->getCode(), $e->getCode(),
str_replace(getenv('PWD'), '***CONFIDENTIAL***', $e->getTraceAsString()) str_replace(getenv('PWD'), '***CONFIDENTIAL***', $e->getTraceAsString())
); );
echo "<br><a href='/'>Go back</a>"; echo "<br><a href='/'>Go back</a>";
}); });
Flight::map('notFound', function() { Flight::map('notFound', function () {
echo '<span id="infotext">Route text:</span> The requested URL was not found<br>'; echo '<span id="infotext">Route text:</span> The requested URL was not found<br>';
echo "<a href='/'>Go back</a>"; echo "<a href='/'>Go back</a>";
}); });
Flight::start(); Flight::start();

@ -1 +1 @@
<span id="infotext">Route text:</span> Template <?=$name?> works! <span id="infotext">Route text:</span> Template <?=$name?> works!
Loading…
Cancel
Save