Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A better FormRequest implementation #3

Merged
merged 4 commits into from
Mar 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 26 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,25 +114,37 @@ Register the service provider in your `config/app.php` as per usual...

Alfheim\Sanitizer\SanitizerServiceProvider::class,

### Helper Traits
### Extending the FormRequest

#### `Alfheim\Sanitizer\Laravel\SanitizesFormRequest`
**This is where the package shines.**
By extending the `Alfheim\Sanitizer\Laravel\FormRequest` on your base `App\Http\Requests\Request` class (instead of the default `Illuminate\Foundation\Http\FormRequest`), you'll be able to define sanitation rules in a `sanitize` method on the given form request, similar to how you define validation rules in the `rules` method.

This trait makes it trivial to add sanitation rules directly onto a `FormRequest`.

Similar to how you define validation rules on a `FormRequest` by defining a `rules`
method which returns an array, you may define sanitation rules by defining a `sanitize`
method which returns an array containing the sanitation rules, like so...
Let me show you in code...

``` php
// app/Http/Requests/Request.php

namespace App\Http\Requests;

use Alfheim\Sanitizer\Laravel\SanitizesFormRequest;
use Alfheim\Sanitizer\Laravel\FormRequest;
// Instead of `Illuminate\Foundation\Http\FormRequest`

class FooRequest extends Request
abstract class Request extends FormRequest
{
use SanitizesFormRequest;
//
}
```

That's it! Now it's trivial to define sanitation rules on your form requests...

``` php
// app/Http/Requests/FooRequest.php

namespace App\Http\Requests;

class FooRequest extends Request
{
// Sanitation rules...
public function sanitize()
{
return [
Expand All @@ -152,7 +164,7 @@ class FooRequest extends Request
}
```

Next, in a controller...
For completeness, I'll show you the controller...

``` php
namespace App\Http\Controllers;
Expand All @@ -164,7 +176,7 @@ class FooController extends Controller
public function create(FooRequest $request)
{
// At this point, the $request will be both sanitized and validated.
// So you may go ahead and access the input as usual:
// You may go ahead and access the input as usual:

$request->all();
$request->input('name');
Expand All @@ -174,6 +186,8 @@ class FooController extends Controller
}
```

### Helper Trait

#### `Alfheim\Sanitizer\Laravel\SanitizesRequests`

This trait adds a `sanitize` method on the class.
Expand Down
4 changes: 1 addition & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
"require-dev": {
"phpunit/phpunit": "~4.1|~5",
"mockery/mockery": "~0.9",
"illuminate/contracts": "^5.2",
"illuminate/support": "^5.2",
"illuminate/http": "^5.2"
"laravel/framework": "^5.2"
},
"autoload": {
"psr-4": {
Expand Down
106 changes: 106 additions & 0 deletions src/Laravel/FormRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

namespace Alfheim\Sanitizer\Laravel;

use Alfheim\Sanitizer\Sanitizer;
use Illuminate\Foundation\Http\FormRequest as BaseFormRequest;

/*
* This class is an extension of the `Illuminate\Foundation\Http\FormRequest`
* class. It provides an easy way to abstract away the sanitation logic from
* your controllers.
*
* A quick example of how it may be used:
*
* To keep it simple, I'll tell our base `App\Http\Requests\Request` class to
* extend `Alfheim\Sanitizer\Laravel\FormRequest` instead of the default
* `Illuminate\Foundation\Http\FormRequest`. Your base request class should look
* something like this:
*
* <?php
* // app/Http/Requests/Request.php
*
* namespace App\Http\Requests;
*
* use Alfheim\Sanitizer\Laravel\FormRequest;
*
* abstract class Request extends FormRequest
* {
* //
* }
* ?>
*
* <?php
* // app/Http/Requests/FooRequest.php
*
* namespace App\Http\Requests;
*
* class FooRequest extends Request
* {
* public function sanitize()
* {
* // This is where you define the rules which will be passed on to
* // the sanitizer.
* return [
* 'name' => 'trim|ucwords',
* 'email' => 'trim|mb_strtolower',
* ];
* }
* }
* ?>
*
* <?php
* // app/Http/Controllers/FooController.php
*
* namespace App\Controllers;
*
* use App\Http\Requests\FooRequest;
*
* class FooController
* {
* public function store(FooRequest $request)
* {
* // At this point, the $request will be both sanitized and
* // validated. So you may go ahead and access the input as usual:
*
* $request->all();
* $request->input('name');
* $request->only(['name', 'email']);
* // etc...
* }
* }
* ?>
*/
abstract class FormRequest extends BaseFormRequest
{
/**
* Perform the sanitation by overriding the
* `Symfony\Component\HttpFoundation::initialize` method. The `$request`
* argument will be sanitized according to the rules defined in the
* `static::sanitize` method.
*
* {@inheritdoc}
*/
public function initialize(array $query = [], array $request = [], array $attributes = [], array $cookies = [], array $files = [], array $server = [], $content = null)
{
if (! empty($request) && ($rules = $this->sanitize())) {
$sanitizer = app(Sanitizer::class)->rules($rules);

$request = $sanitizer->sanitize($request);
}

parent::initialize(
$query, $request, $attributes, $cookies, $files, $server, $content
);
}

/**
* Get the sanitation rules for this form request.
*
* @return array
*/
public function sanitize()
{
return [];
}
}
95 changes: 0 additions & 95 deletions src/Laravel/SanitizesFormRequest.php

This file was deleted.

Loading