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

Adds a section for integration in mvc and expressive applications #183

Closed
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
# Usage in a zend-expressive Application

The following example shows _one_ potential use case of zend-inputfilter within
a zend-expressive based application. The example uses a module, config provider
configuration, zend-servicemanager as dependency injection container, the
zend-inputfilter plugin manager and a request handler.

Before starting, make sure zend-inputfilter is installed and configured.

## Create Input Filter

Create an input filter as separate class, e.g.
`src/Album/InputFilter/QueryInputFilter.php`:

```php
namespace Album\InputFilter;

use Zend\Filter\ToInt;
use Zend\I18n\Validator\IsInt;
use Zend\InputFilter\InputFilter;

class QueryInputFilter extends InputFilter
{
public function init()
{
// Page
$this->add(
[
'name' => 'page',
'allow_empty' => true,
'validators' => [
[
'name' => IsInt::class,
],
],
'filters' => [
[
'name' => ToInt::class,
],
],
'fallback_value' => 1,
]
);

// …
}
}
```

## Using Input Filter

### Create Handler

Using the input filter in a request handler, e.g.
`src/Album/Handler/ListHandler.php`:

```php
namespace Album\Handler;

use Album\InputFilter\QueryInputFilter;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Http\Message\ResponseInterface;
use Zend\InputFilter\InputFilterInterface;
use Zend\Expressive\Template\TemplateRendererInterface;

class ListHandler implements RequestHandlerInterface
{
/** @var InputFilterInterface */
private $inputFilter;

/** @var TemplateRendererInterface */
private $renderer;

public function __construct(
InputFilterInterface $inputFilter,
TemplateRendererInterface $renderer
) {
$this->inputFilter = $inputFilter;
$this->renderer = $renderer;
}

public function handle(ServerRequestInterface $request) : ResponseInterface
{
$this->inputFilter->setData($request->getQueryParams());
$this->inputFilter->isValid();
$filteredParams = $this->inputFilter->getValues();

// …

return new HtmlResponse($this->renderer->render(
'album::list',
[]
));
}
}
```

### Create Factory for Handler

Fetch the `QueryInputFilter` from the input filter plugin manager in a factory,
e.g. `src/Album/Handler/ListHandlerFactory.php`:

```php
namespace Album\Handler;

use Album\InputFilter\QueryInputFilter;
use Psr\Container\ContainerInterface;
use Zend\InputFilter\InputFilterPluginManager;

class ListHandlerFactory
{
public function __invoke(ContainerInterface $container)
{
/** @var InputFilterPluginManager $pluginManager */
$pluginManager = $container->get(InputFilterPluginManager::class);
$inputFilter = $pluginManager->get(QueryInputFilter::class);

return new ListHandler(
$inputFilter,
$container->get(TemplateRendererInterface::class)
);
}
}
```

> ### Instantiating the InputFilter
>
> The `InputFilterPluginManager` is used instead of directly instantiating the
> input filter to ensure to get the filter and validator plugin managers
> injected. This allows usage of any filters and validators registered with
> their respective plugin managers.
>
> Additionally the `InputFilterPluginManager` calls the `init` method _after_
> instantiating the input filter, ensuring all dependencies are fully injected
> first.

## Register Input Filter and Handler

Extend the configuration provider of the module to register the input filter and
the request handler, e.g. `src/Album/ConfigProvider.php`:

```php
namespace Album;

class ConfigProvider
{
public function __invoke() : array
{
return [
'dependencies' => $this->getDependencies(),
'input_filters' => $this->getInputFilters(), // <-- Add this line
];
}

public function getDependencies() : array
{
return [
'factories' => [
Handler\ListHandler::class => Handler\ListHandlerFactory::class, // <-- Add this line
// …
],
];
}

// Add the following method
public function getInputFilters() : array
{
return [
'factories' => [
InputFilter\QueryInputFilter::class => InvokableFactory::class,
],
];
}

// …
}
```
149 changes: 149 additions & 0 deletions docs/book/application-integration/usage-in-a-zend-mvc-application.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
# Usage in a zend-mvc Application

The following example shows _one_ potential use case of zend-inputfilter within
a zend-mvc based application. The example uses a module, a controller and the
zend-inputfilter plugin manager.

Before starting, make sure zend-inputfilter is installed and configured.

## Create Input Filter

Create an input filter as separate class, e.g.
`module/Album/src/InputFilter/QueryInputFilter.php`:

```php
namespace Album\InputFilter;

use Zend\Filter\ToInt;
use Zend\I18n\Validator\IsInt;
use Zend\InputFilter\InputFilter;

class QueryInputFilter extends InputFilter
{
public function init()
{
// Page
$this->add(
[
'name' => 'page',
'allow_empty' => true,
'validators' => [
[
'name' => IsInt::class,
],
],
'filters' => [
[
'name' => ToInt::class,
],
],
'fallback_value' => 1,
]
);

// …
}
}
```

## Using Input Filter

### Create Controller

Using the input filter in a controller, e.g.
`module/Album/Controller/AlbumController.php`:

```php
namespace Album\Controller;

use Album\InputFilter\QueryInputFilter;
use Zend\InputFilter\InputFilterInterface;
use Zend\Mvc\Controller\AbstractActionController;

class AlbumController extends AbstractActionController
{
/** @var InputFilterInterface */
private $inputFilter;

public function __construct(InputFilterInterface $inputFilter)
{
$this->inputFilter = $inputFilter;
}

public function indexAction()
{
$this->inputFilter->setData($this->getRequest()->getQuery());
$this->inputFilter->isValid();
$filteredParams = $this->inputFilter->getValues();

// …
}
}
```

### Create Factory for Controller

Fetch the `QueryInputFilter` from the input filter plugin manager in a factory,
e.g. `src/Album/Handler/ListHandlerFactory.php`:

```php
namespace Album\Controller;

use Album\InputFilter\QueryInputFilter;
use Interop\Container\ContainerInterface;
use Zend\InputFilter\InputFilterPluginManager;
use Zend\ServiceManager\Factory\FactoryInterface;

class AlbumControllerFactory implements FactoryInterface
{
public function __invoke(
ContainerInterface $container,
$requestedName,
array $options = null
) {
/** @var InputFilterPluginManager $pluginManager */
$pluginManager = $container->get(InputFilterPluginManager::class);
$inputFilter = $pluginManager->get(QueryInputFilter::class);

return new AlbumController($inputFilter);
}
}
```

> ### Instantiating the Input Filter
>
> The `InputFilterPluginManager` is used instead of directly instantiating the
> input filter to ensure to get the filter and validator plugin managers
> injected. This allows usage of any filters and validators registered with
> their respective plugin managers.
>
> Additionally the `InputFilterPluginManager` calls the `init` method _after_
> instantiating the input filter, ensuring all dependencies are fully injected
> first.

## Register Input Filter and Controller

Extend the configuration of the module to register the input filter and
controller in the application, e.g. `module/Album/config/module.config.php`:

```php
namespace Album;

use Zend\ServiceManager\Factory\InvokableFactory;

return [
'controllers' => [
'factories' => [
// Add this line
Controller\AlbumController::class => Controller\AlbumControllerFactory::class,
],
],
// Add the following array
'input_filters' => [
'factories => [
InputFilter\QueryInputFilter::class => InvokableFactory::class,
],
],
// …
];
```
3 changes: 3 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ pages:
- Files: file-input.md
- "Optional Input Filters": optional-input-filters.md
- "Unfiltered Data": unfiltered-data.md
- "Application Integration":
- "Usage in a zend-mvc application": application-integration/usage-in-a-zend-mvc-application.md
- "Usage in a zend-expressive application": application-integration/usage-in-a-zend-expressive-application.md
site_name: zend-inputfilter
site_description: zend-inputfilter
repo_url: 'https://github.com/zendframework/zend-inputfilter'
Expand Down