-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRequest.php
73 lines (61 loc) · 1.78 KB
/
Request.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
namespace Core\Facades;
class Request{
/**
* Get the current request URI
* @return string
*/
public static function getRequestURI(){
$requestURI = $_SERVER["REQUEST_URI"];
$requestURI = strlen($_ENV["SUB_DIRECTORY"]) > 0 ? str_replace('/'.$_ENV["SUB_DIRECTORY"].'/', '', $requestURI) : str_replace('/', '', $requestURI);
$requestURI = trim($requestURI, '/');
return $requestURI;
}
/**
* get all request parameters
* @return array
*/
public static function all(){
self::sanitizeParams();
return $_SERVER["REQUEST_METHOD"] == 'POST' ? $_POST : $_GET;
}
/**
* get certain request parameters
* @return array
*/
public static function only($wanted){
$all = self::all();
$result = [];
foreach ($all as $key => $value) {
if(in_array($key, $wanted)){
$result += [$key => $value];
}
}
return $result;
}
/**
* redirect to a given location
*/
public static function redirect($uri){
return header('Location: ' . self::url($uri));
}
/**
* sanitizes a post-request inputs
* @return void
*/
private static function sanitizeParams(){
$_GET = filter_input_array(INPUT_GET, FILTER_SANITIZE_STRING);
$_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
}
/**
* get a full url from a given path
* @return string
*/
public static function url($path){
# Ensure APP_URL does not have a trailing slash
$baseUrl = rtrim($_ENV['APP_URL'], '/');
# Ensure $path does not have a leading slash
$path = ltrim($path, '/');
return $baseUrl . '/' . $path;
}
}