1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Alfheim\Sanitizer\Laravel; |
4
|
|
|
|
5
|
|
|
use Alfheim\Sanitizer\Sanitizer; |
6
|
|
|
use Illuminate\Foundation\Http\FormRequest as BaseFormRequest; |
7
|
|
|
|
8
|
|
|
/* |
9
|
|
|
* This class is an extension of the `Illuminate\Foundation\Http\FormRequest` |
10
|
|
|
* class. It provides an easy way to abstract away the sanitation logic from |
11
|
|
|
* your controllers. |
12
|
|
|
* |
13
|
|
|
* A quick example of how it may be used: |
14
|
|
|
* |
15
|
|
|
* To keep it simple, I'll tell our base `App\Http\Requests\Request` class to |
16
|
|
|
* extend `Alfheim\Sanitizer\Laravel\FormRequest` instead of the default |
17
|
|
|
* `Illuminate\Foundation\Http\FormRequest`. Your base request class should look |
18
|
|
|
* something like this: |
19
|
|
|
* |
20
|
|
|
* <?php |
21
|
|
|
* // app/Http/Requests/Request.php |
22
|
|
|
* |
23
|
|
|
* namespace App\Http\Requests; |
24
|
|
|
* |
25
|
|
|
* use Alfheim\Sanitizer\Laravel\FormRequest; |
26
|
|
|
* |
27
|
|
|
* abstract class Request extends FormRequest |
28
|
|
|
* { |
29
|
|
|
* // |
30
|
|
|
* } |
31
|
|
|
* ?> |
32
|
|
|
* |
33
|
|
|
* <?php |
34
|
|
|
* // app/Http/Requests/FooRequest.php |
35
|
|
|
* |
36
|
|
|
* namespace App\Http\Requests; |
37
|
|
|
* |
38
|
|
|
* class FooRequest extends Request |
39
|
|
|
* { |
40
|
|
|
* public function sanitize() |
41
|
|
|
* { |
42
|
|
|
* // This is where you define the rules which will be passed on to |
43
|
|
|
* // the sanitizer. |
44
|
|
|
* return [ |
45
|
|
|
* 'name' => 'trim|ucwords', |
46
|
|
|
* 'email' => 'trim|mb_strtolower', |
47
|
|
|
* ]; |
48
|
|
|
* } |
49
|
|
|
* } |
50
|
|
|
* ?> |
51
|
|
|
* |
52
|
|
|
* <?php |
53
|
|
|
* // app/Http/Controllers/FooController.php |
54
|
|
|
* |
55
|
|
|
* namespace App\Controllers; |
56
|
|
|
* |
57
|
|
|
* use App\Http\Requests\FooRequest; |
58
|
|
|
* |
59
|
|
|
* class FooController |
60
|
|
|
* { |
61
|
|
|
* public function store(FooRequest $request) |
62
|
|
|
* { |
63
|
|
|
* // At this point, the $request will be both sanitized and |
64
|
|
|
* // validated. So you may go ahead and access the input as usual: |
65
|
|
|
* |
66
|
|
|
* $request->all(); |
67
|
|
|
* $request->input('name'); |
68
|
|
|
* $request->only(['name', 'email']); |
69
|
|
|
* // etc... |
70
|
|
|
* } |
71
|
|
|
* } |
72
|
|
|
* ?> |
73
|
|
|
*/ |
74
|
|
|
abstract class FormRequest extends BaseFormRequest |
75
|
|
|
{ |
76
|
|
|
/** |
77
|
|
|
* Perform the sanitation by overriding the |
78
|
|
|
* `Symfony\Component\HttpFoundation::initialize` method. The `$request` |
79
|
|
|
* argument will be sanitized according to the rules defined in the |
80
|
|
|
* `static::sanitize` method. |
81
|
|
|
* |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
*/ |
84
|
24 |
|
public function initialize(array $query = [], array $request = [], array $attributes = [], array $cookies = [], array $files = [], array $server = [], $content = null) |
85
|
|
|
{ |
86
|
24 |
|
if (! empty($request) && ($rules = $this->sanitize())) { |
87
|
20 |
|
$sanitizer = app(Sanitizer::class)->rules($rules); |
88
|
|
|
|
89
|
20 |
|
$request = $sanitizer->sanitize($request); |
90
|
15 |
|
} |
91
|
|
|
|
92
|
24 |
|
parent::initialize( |
93
|
18 |
|
$query, $request, $attributes, $cookies, $files, $server, $content |
94
|
18 |
|
); |
95
|
24 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Get the sanitation rules for this form request. |
99
|
|
|
* |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
4 |
|
public function sanitize() |
103
|
|
|
{ |
104
|
4 |
|
return []; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|