Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

Commit 295db58

Browse files
committed
Adds a section for integration in mvc and expressive applications
1 parent 74d19b8 commit 295db58

File tree

3 files changed

+363
-0
lines changed

3 files changed

+363
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
# Usage in a zend-expressive Application
2+
3+
The following example shows _one_ usage of zend-inputfilter in a zend-expressive
4+
based application. The example uses a module, config provider configuration,
5+
zend-servicemanager as dependency injection container, the zend-inputfilter
6+
plugin manager and a request handler.
7+
8+
## Register Configuration Provider
9+
10+
Add the configuration provider of zend-inputfilter to your configuration file,
11+
e.g. `config/config.php`:
12+
13+
```php
14+
$aggregator = new Zend\ConfigAggregator\ConfigAggregator([
15+
// …
16+
Zend\Expressive\ConfigProvider::class,
17+
Zend\Expressive\Router\ConfigProvider::class,
18+
Zend\InputFilter\ConfigProvider::class, // <-- Add this line
19+
//
20+
]);
21+
```
22+
23+
## Create Input-Filter
24+
25+
Create an input-filter as separate class, e.g.
26+
`src/Album/InputFilter/QueryInputFilter.php`:
27+
28+
```php
29+
namespace Album\InputFilter;
30+
31+
use Zend\Filter\ToInt;
32+
use Zend\I18n\Validator\IsInt;
33+
use Zend\InputFilter\InputFilter;
34+
35+
class QueryInputFilter extends InputFilter
36+
{
37+
public function init()
38+
{
39+
// Page
40+
$this->add(
41+
[
42+
'name' => 'page',
43+
'allow_empty' => true,
44+
'validators' => [
45+
[
46+
'name' => IsInt::class,
47+
],
48+
],
49+
'filters' => [
50+
[
51+
'name' => ToInt::class,
52+
],
53+
],
54+
'fallback_value' => 1,
55+
]
56+
);
57+
58+
// …
59+
}
60+
}
61+
```
62+
63+
## Register Input-Filter
64+
65+
Extend the configuration provider of the module to register the input-filter,
66+
e.g. `src/Album/ConfigProvider.php`:
67+
68+
```php
69+
namespace Album;
70+
71+
use Zend\ServiceManager\Factory\InvokableFactory;
72+
73+
class ConfigProvider
74+
{
75+
public function __invoke() : array
76+
{
77+
return [
78+
'dependencies' => $this->getDependencies(),
79+
'input_filters' => $this->getInputFilters(), // <-- Add this line
80+
];
81+
}
82+
83+
// Add the following method
84+
public function getInputFilters() : array
85+
{
86+
return [
87+
'factories' => [
88+
InputFilter\QueryInputFilter::class => InvokableFactory::class,
89+
],
90+
];
91+
}
92+
93+
// …
94+
}
95+
```
96+
97+
## Using Input-Filter
98+
99+
### Create Handler
100+
101+
Using the input-filter in a request handler, e.g.
102+
`src/Album/Handler/ListHandler.php`:
103+
104+
```php
105+
namespace Album\Handler;
106+
107+
use Album\InputFilter\QueryInputFilter;
108+
use Psr\Http\Message\ServerRequestInterface;
109+
use Psr\Http\Server\RequestHandlerInterface;
110+
use Psr\Http\Message\ResponseInterface;
111+
use Zend\InputFilter\InputFilterInterface;
112+
113+
class ListHandler implements RequestHandlerInterface
114+
{
115+
/** @var InputFilterInterface */
116+
private $inputFilter;
117+
118+
public function __construct(InputFilterInterface $inputFilter)
119+
{
120+
$this->inputFilter = $inputFilter;
121+
}
122+
123+
public function handle(ServerRequestInterface $request) : ResponseInterface
124+
{
125+
$this->inputFilter->setData($request->getQueryParams());
126+
$this->inputFilter->isValid();
127+
$filteredParams = $this->inputFilter->getValues();
128+
129+
// …
130+
}
131+
}
132+
```
133+
134+
### Create Factory for Handler
135+
136+
Fetch the `QueryInputFilter` from the input-filter plugin manager in a factory,
137+
e.g. `src/Album/Handler/ListHandlerFactory.php`:
138+
139+
```php
140+
namespace Album\Handler;
141+
142+
use Album\InputFilter\QueryInputFilter;
143+
use Psr\Container\ContainerInterface;
144+
use Zend\InputFilter\InputFilterPluginManager;
145+
146+
class ListHandlerFactory
147+
{
148+
public function __invoke(ContainerInterface $container)
149+
{
150+
/** @var InputFilterPluginManager $pluginManager */
151+
$pluginManager = $container->get(InputFilterPluginManager::class);
152+
$inputFilter = $pluginManager->get(QueryInputFilter::class);
153+
154+
return new ListHandler($inputFilter);
155+
}
156+
}
157+
```
158+
159+
> The `InputFilterPluginManager` calls the `init` method _after_ instantiating
160+
the input-filter and injecting it with a factory composing all the various
161+
plugin-manager services.
162+
163+
### Register Handler
164+
165+
Extend the configuration provider of the module to register the input-filter,
166+
e.g. `src/Album/ConfigProvider.php`:
167+
168+
```php
169+
namespace Album;
170+
171+
class ConfigProvider
172+
{
173+
public function __invoke() : array
174+
{
175+
return [
176+
'dependencies' => $this->getDependencies(),
177+
'input_filters' => $this->getInputFilters(),
178+
];
179+
}
180+
181+
public function getDependencies() : array
182+
{
183+
return [
184+
'factories' => [
185+
Handler\ListHandler::class => Handler\ListHandlerFactory::class, // <-- Add this line
186+
],
187+
];
188+
}
189+
190+
//
191+
}
192+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
# Usage in a zend-mvc Application
2+
3+
The following example shows _one_ usage of zend-inputfilter in a zend-mvc
4+
based application. The example uses a module, a controller and the
5+
zend-inputfilter plugin manager.
6+
7+
## Register Module
8+
9+
Add zend-inputfilter as module to your configuration file,
10+
e.g. `config/modules.config.php`:
11+
12+
```php
13+
return [
14+
'Zend\Router',
15+
'Zend\Session',
16+
'Zend\Validator',
17+
'Zend\InputFilter', // <-- Add this line
18+
'Application',
19+
];
20+
```
21+
22+
## Create Input-Filter
23+
24+
Create an input-filter as separate class, e.g.
25+
`module/Album/src/InputFilter/QueryInputFilter.php`:
26+
27+
```php
28+
namespace Album\InputFilter;
29+
30+
use Zend\Filter\ToInt;
31+
use Zend\I18n\Validator\IsInt;
32+
use Zend\InputFilter\InputFilter;
33+
34+
class QueryInputFilter extends InputFilter
35+
{
36+
public function init()
37+
{
38+
// Page
39+
$this->add(
40+
[
41+
'name' => 'page',
42+
'allow_empty' => true,
43+
'validators' => [
44+
[
45+
'name' => IsInt::class,
46+
],
47+
],
48+
'filters' => [
49+
[
50+
'name' => ToInt::class,
51+
],
52+
],
53+
'fallback_value' => 1,
54+
]
55+
);
56+
57+
// …
58+
}
59+
}
60+
```
61+
62+
## Register Input-Filter
63+
64+
Extend the configuration of the module to register the input-filter,
65+
e.g. `module/Album/config/module.config.php`:
66+
67+
```php
68+
namespace Album;
69+
70+
use Zend\ServiceManager\Factory\InvokableFactory;
71+
72+
return [
73+
// Add the following array
74+
'input_filters' => [
75+
'factories => [
76+
InputFilter\QueryInputFilter::class => InvokableFactory::class,
77+
],
78+
],
79+
// …
80+
];
81+
```
82+
83+
## Using Input-Filter
84+
85+
### Create Controller
86+
87+
Using the input-filter in a controller, e.g.
88+
`module/Album/Controller/AlbumController.php`:
89+
90+
```php
91+
namespace Album\Controller;
92+
93+
use Album\InputFilter\QueryInputFilter;
94+
use Zend\InputFilter\InputFilterInterface;
95+
use Zend\Mvc\Controller\AbstractActionController;
96+
97+
class AlbumController extends AbstractActionController
98+
{
99+
/** @var InputFilterInterface */
100+
private $inputFilter;
101+
102+
public function __construct(InputFilterInterface $inputFilter)
103+
{
104+
$this->inputFilter = $inputFilter;
105+
}
106+
107+
public function indexAction()
108+
{
109+
$this->inputFilter->setData($this->getRequest()->getQuery());
110+
$this->inputFilter->isValid();
111+
$filteredParams = $this->inputFilter->getValues();
112+
113+
// …
114+
}
115+
}
116+
```
117+
118+
### Create Factory for Controller
119+
120+
Fetch the `QueryInputFilter` from the input-filter plugin manager in a factory,
121+
e.g. `src/Album/Handler/ListHandlerFactory.php`:
122+
123+
```php
124+
namespace Album\Controller;
125+
126+
use Album\InputFilter\QueryInputFilter;
127+
use Interop\Container\ContainerInterface;
128+
use Zend\InputFilter\InputFilterPluginManager;
129+
use Zend\ServiceManager\Factory\FactoryInterface;
130+
131+
class AlbumControllerFactory implements FactoryInterface
132+
{
133+
public function __invoke(
134+
ContainerInterface $container,
135+
$requestedName,
136+
array $options = null
137+
) {
138+
/** @var InputFilterPluginManager $pluginManager */
139+
$pluginManager = $container->get(InputFilterPluginManager::class);
140+
$inputFilter = $pluginManager->get(QueryInputFilter::class);
141+
142+
return new AlbumController($inputFilter);
143+
}
144+
}
145+
```
146+
147+
> The `InputFilterPluginManager` calls the `init` method _after_ instantiating
148+
the input-filter and injecting it with a factory composing all the various
149+
plugin-manager services.
150+
151+
### Register Controller
152+
153+
Extend the configuration of the module to register the controller,
154+
e.g. `module/Album/config/module.config.php`:
155+
156+
```php
157+
namespace Album;
158+
159+
return [
160+
// Add the following array
161+
'controllers' => [
162+
'factories' => [
163+
Controller\AlbumController::class => Controller\AlbumControllerFactory::class,
164+
],
165+
],
166+
// …
167+
];
168+
```

mkdocs.yml

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ pages:
88
- "File Upload Input": file-input.md
99
- "Optional Input Filters": optional-input-filters.md
1010
- "Unfiltered Data": unfiltered-data.md
11+
- "Application Integration":
12+
- "Usage in a zend-mvc application": application-integration/usage-in-a-zend-mvc-application.md
13+
- "Usage in a zend-expressive application": application-integration/usage-in-a-zend-expressive-application.md
1114
site_name: zend-inputfilter
1215
site_description: 'Normalize and validate input sets from the web, APIs, the CLI, and more, including files.'
1316
repo_url: 'https://github.com/zendframework/zend-inputfilter'

0 commit comments

Comments
 (0)