From 7277e8387776009d42f344d111f2ba61eab870da Mon Sep 17 00:00:00 2001 From: frank Date: Thu, 23 May 2019 21:15:52 +0200 Subject: [PATCH 01/14] Adds a section for integration in mvc and expressive applications --- .../usage-in-a-zend-expressive-application.md | 192 ++++++++++++++++++ .../usage-in-a-zend-mvc-application.md | 168 +++++++++++++++ mkdocs.yml | 3 + 3 files changed, 363 insertions(+) create mode 100644 docs/book/application-integration/usage-in-a-zend-expressive-application.md create mode 100644 docs/book/application-integration/usage-in-a-zend-mvc-application.md diff --git a/docs/book/application-integration/usage-in-a-zend-expressive-application.md b/docs/book/application-integration/usage-in-a-zend-expressive-application.md new file mode 100644 index 00000000..9ce787aa --- /dev/null +++ b/docs/book/application-integration/usage-in-a-zend-expressive-application.md @@ -0,0 +1,192 @@ +# Usage in a zend-expressive Application + +The following example shows _one_ usage of zend-inputfilter in 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. + +## Register Configuration Provider + +Add the configuration provider of zend-inputfilter to your configuration file, +e.g. `config/config.php`: + +```php +$aggregator = new Zend\ConfigAggregator\ConfigAggregator([ + // … + Zend\Expressive\ConfigProvider::class, + Zend\Expressive\Router\ConfigProvider::class, + Zend\InputFilter\ConfigProvider::class, // <-- Add this line + // … +]); +``` + +## 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, + ] + ); + + // … + } +} +``` + +## Register Input-Filter + +Extend the configuration provider of the module to register the input-filter, +e.g. `src/Album/ConfigProvider.php`: + +```php +namespace Album; + +use Zend\ServiceManager\Factory\InvokableFactory; + +class ConfigProvider +{ + public function __invoke() : array + { + return [ + 'dependencies' => $this->getDependencies(), + 'input_filters' => $this->getInputFilters(), // <-- Add this line + ]; + } + + // Add the following method + public function getInputFilters() : array + { + return [ + 'factories' => [ + InputFilter\QueryInputFilter::class => InvokableFactory::class, + ], + ]; + } + + // … +} +``` + +## 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; + +class ListHandler implements RequestHandlerInterface +{ + /** @var InputFilterInterface */ + private $inputFilter; + + public function __construct(InputFilterInterface $inputFilter) + { + $this->inputFilter = $inputFilter; + } + + public function handle(ServerRequestInterface $request) : ResponseInterface + { + $this->inputFilter->setData($request->getQueryParams()); + $this->inputFilter->isValid(); + $filteredParams = $this->inputFilter->getValues(); + + // … + } +} +``` + +### 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); + } +} +``` + +> The `InputFilterPluginManager` calls the `init` method _after_ instantiating +the input-filter and injecting it with a factory composing all the various +plugin-manager services. + +### Register Handler + +Extend the configuration provider of the module to register the input-filter, +e.g. `src/Album/ConfigProvider.php`: + +```php +namespace Album; + +class ConfigProvider +{ + public function __invoke() : array + { + return [ + 'dependencies' => $this->getDependencies(), + 'input_filters' => $this->getInputFilters(), + ]; + } + + public function getDependencies() : array + { + return [ + 'factories' => [ + Handler\ListHandler::class => Handler\ListHandlerFactory::class, // <-- Add this line + ], + ]; + } + + // … +} +``` diff --git a/docs/book/application-integration/usage-in-a-zend-mvc-application.md b/docs/book/application-integration/usage-in-a-zend-mvc-application.md new file mode 100644 index 00000000..388dfa31 --- /dev/null +++ b/docs/book/application-integration/usage-in-a-zend-mvc-application.md @@ -0,0 +1,168 @@ +# Usage in a zend-mvc Application + +The following example shows _one_ usage of zend-inputfilter in a zend-mvc +based application. The example uses a module, a controller and the +zend-inputfilter plugin manager. + +## Register Module + +Add zend-inputfilter as module to your configuration file, +e.g. `config/modules.config.php`: + +```php +return [ + 'Zend\Router', + 'Zend\Session', + 'Zend\Validator', + 'Zend\InputFilter', // <-- Add this line + 'Application', +]; +``` + +## 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, + ] + ); + + // … + } +} +``` + +## Register Input-Filter + +Extend the configuration of the module to register the input-filter, +e.g. `module/Album/config/module.config.php`: + +```php +namespace Album; + +use Zend\ServiceManager\Factory\InvokableFactory; + +return [ + // Add the following array + 'input_filters' => [ + 'factories => [ + InputFilter\QueryInputFilter::class => InvokableFactory::class, + ], + ], + // … +]; +``` + +## 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); + } +} +``` + +> The `InputFilterPluginManager` calls the `init` method _after_ instantiating +the input-filter and injecting it with a factory composing all the various +plugin-manager services. + +### Register Controller + +Extend the configuration of the module to register the controller, +e.g. `module/Album/config/module.config.php`: + +```php +namespace Album; + +return [ + // Add the following array + 'controllers' => [ + 'factories' => [ + Controller\AlbumController::class => Controller\AlbumControllerFactory::class, + ], + ], + // … +]; +``` diff --git a/mkdocs.yml b/mkdocs.yml index 6e056c3c..ab6d22e3 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -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' From 66c1dc9bef2229753533b1c444c4a842ae9d6b4f Mon Sep 17 00:00:00 2001 From: frank Date: Fri, 23 Aug 2019 15:20:47 +0200 Subject: [PATCH 02/14] Removes parts of the installation process from usage descriptions --- .../usage-in-a-zend-expressive-application.md | 15 +-------------- .../usage-in-a-zend-mvc-application.md | 15 +-------------- 2 files changed, 2 insertions(+), 28 deletions(-) diff --git a/docs/book/application-integration/usage-in-a-zend-expressive-application.md b/docs/book/application-integration/usage-in-a-zend-expressive-application.md index 9ce787aa..2bd7593a 100644 --- a/docs/book/application-integration/usage-in-a-zend-expressive-application.md +++ b/docs/book/application-integration/usage-in-a-zend-expressive-application.md @@ -5,20 +5,7 @@ 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. -## Register Configuration Provider - -Add the configuration provider of zend-inputfilter to your configuration file, -e.g. `config/config.php`: - -```php -$aggregator = new Zend\ConfigAggregator\ConfigAggregator([ - // … - Zend\Expressive\ConfigProvider::class, - Zend\Expressive\Router\ConfigProvider::class, - Zend\InputFilter\ConfigProvider::class, // <-- Add this line - // … -]); -``` +Before starting, make sure zend-inputfilter is installed and configured. ## Create Input-Filter diff --git a/docs/book/application-integration/usage-in-a-zend-mvc-application.md b/docs/book/application-integration/usage-in-a-zend-mvc-application.md index 388dfa31..703a9ff4 100644 --- a/docs/book/application-integration/usage-in-a-zend-mvc-application.md +++ b/docs/book/application-integration/usage-in-a-zend-mvc-application.md @@ -4,20 +4,7 @@ The following example shows _one_ usage of zend-inputfilter in a zend-mvc based application. The example uses a module, a controller and the zend-inputfilter plugin manager. -## Register Module - -Add zend-inputfilter as module to your configuration file, -e.g. `config/modules.config.php`: - -```php -return [ - 'Zend\Router', - 'Zend\Session', - 'Zend\Validator', - 'Zend\InputFilter', // <-- Add this line - 'Application', -]; -``` +Before starting, make sure zend-inputfilter is installed and configured. ## Create Input-Filter From 70c7992deb8ed977d4a9794f70b7534cd7a2c1ef Mon Sep 17 00:00:00 2001 From: frank Date: Fri, 23 Aug 2019 15:22:08 +0200 Subject: [PATCH 03/14] Adds headlines to blockquotes in usage descriptions --- .../usage-in-a-zend-expressive-application.md | 2 ++ .../application-integration/usage-in-a-zend-mvc-application.md | 2 ++ 2 files changed, 4 insertions(+) diff --git a/docs/book/application-integration/usage-in-a-zend-expressive-application.md b/docs/book/application-integration/usage-in-a-zend-expressive-application.md index 2bd7593a..05b876e3 100644 --- a/docs/book/application-integration/usage-in-a-zend-expressive-application.md +++ b/docs/book/application-integration/usage-in-a-zend-expressive-application.md @@ -143,6 +143,8 @@ class ListHandlerFactory } ``` +> ### Instantiating the Input-Filter +> > The `InputFilterPluginManager` calls the `init` method _after_ instantiating the input-filter and injecting it with a factory composing all the various plugin-manager services. diff --git a/docs/book/application-integration/usage-in-a-zend-mvc-application.md b/docs/book/application-integration/usage-in-a-zend-mvc-application.md index 703a9ff4..a7a594f6 100644 --- a/docs/book/application-integration/usage-in-a-zend-mvc-application.md +++ b/docs/book/application-integration/usage-in-a-zend-mvc-application.md @@ -131,6 +131,8 @@ class AlbumControllerFactory implements FactoryInterface } ``` +> ### Instantiating the Input-Filter +> > The `InputFilterPluginManager` calls the `init` method _after_ instantiating the input-filter and injecting it with a factory composing all the various plugin-manager services. From 11802875330d2efe4b3073a9f1ece83c73d49383 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frank=20Bru=CC=88ckner?= Date: Mon, 28 Oct 2019 22:52:01 +0100 Subject: [PATCH 04/14] Fixes spelling of input filter --- .../usage-in-a-zend-expressive-application.md | 18 +++++++++--------- .../usage-in-a-zend-mvc-application.md | 8 ++++---- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/docs/book/application-integration/usage-in-a-zend-expressive-application.md b/docs/book/application-integration/usage-in-a-zend-expressive-application.md index 05b876e3..fa56da1e 100644 --- a/docs/book/application-integration/usage-in-a-zend-expressive-application.md +++ b/docs/book/application-integration/usage-in-a-zend-expressive-application.md @@ -7,9 +7,9 @@ plugin manager and a request handler. Before starting, make sure zend-inputfilter is installed and configured. -## Create Input-Filter +## Create InputFilter -Create an input-filter as separate class, e.g. +Create an input filter as separate class, e.g. `src/Album/InputFilter/QueryInputFilter.php`: ```php @@ -47,9 +47,9 @@ class QueryInputFilter extends InputFilter } ``` -## Register Input-Filter +## Register InputFilter -Extend the configuration provider of the module to register the input-filter, +Extend the configuration provider of the module to register the input filter, e.g. `src/Album/ConfigProvider.php`: ```php @@ -81,11 +81,11 @@ class ConfigProvider } ``` -## Using Input-Filter +## Using InputFilter ### Create Handler -Using the input-filter in a request handler, e.g. +Using the input filter in a request handler, e.g. `src/Album/Handler/ListHandler.php`: ```php @@ -120,7 +120,7 @@ class ListHandler implements RequestHandlerInterface ### Create Factory for Handler -Fetch the `QueryInputFilter` from the input-filter plugin manager in a factory, +Fetch the `QueryInputFilter` from the input filter plugin manager in a factory, e.g. `src/Album/Handler/ListHandlerFactory.php`: ```php @@ -143,7 +143,7 @@ class ListHandlerFactory } ``` -> ### Instantiating the Input-Filter +> ### Instantiating the InputFilter > > The `InputFilterPluginManager` calls the `init` method _after_ instantiating the input-filter and injecting it with a factory composing all the various @@ -151,7 +151,7 @@ plugin-manager services. ### Register Handler -Extend the configuration provider of the module to register the input-filter, +Extend the configuration provider of the module to register the input filter, e.g. `src/Album/ConfigProvider.php`: ```php diff --git a/docs/book/application-integration/usage-in-a-zend-mvc-application.md b/docs/book/application-integration/usage-in-a-zend-mvc-application.md index a7a594f6..93b6b412 100644 --- a/docs/book/application-integration/usage-in-a-zend-mvc-application.md +++ b/docs/book/application-integration/usage-in-a-zend-mvc-application.md @@ -71,7 +71,7 @@ return [ ### Create Controller -Using the input-filter in a controller, e.g. +Using the input filter in a controller, e.g. `module/Album/Controller/AlbumController.php`: ```php @@ -104,7 +104,7 @@ class AlbumController extends AbstractActionController ### Create Factory for Controller -Fetch the `QueryInputFilter` from the input-filter plugin manager in a factory, +Fetch the `QueryInputFilter` from the input filter plugin manager in a factory, e.g. `src/Album/Handler/ListHandlerFactory.php`: ```php @@ -131,10 +131,10 @@ class AlbumControllerFactory implements FactoryInterface } ``` -> ### Instantiating the Input-Filter +> ### Instantiating the InputFilter > > The `InputFilterPluginManager` calls the `init` method _after_ instantiating -the input-filter and injecting it with a factory composing all the various +the input filter and injecting it with a factory composing all the various plugin-manager services. ### Register Controller From 6676fb1f13e08431787460723e2dfe3c57bf0f61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frank=20Bru=CC=88ckner?= Date: Mon, 28 Oct 2019 22:56:47 +0100 Subject: [PATCH 05/14] Updates the description for instantiating the input filter --- .../usage-in-a-zend-expressive-application.md | 9 ++++++--- .../usage-in-a-zend-mvc-application.md | 9 ++++++--- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/docs/book/application-integration/usage-in-a-zend-expressive-application.md b/docs/book/application-integration/usage-in-a-zend-expressive-application.md index fa56da1e..41e66dbc 100644 --- a/docs/book/application-integration/usage-in-a-zend-expressive-application.md +++ b/docs/book/application-integration/usage-in-a-zend-expressive-application.md @@ -145,9 +145,12 @@ class ListHandlerFactory > ### Instantiating the InputFilter > -> The `InputFilterPluginManager` calls the `init` method _after_ instantiating -the input-filter and injecting it with a factory composing all the various -plugin-manager services. +> The `InputFilterPluginManager` is used instead of directly instantiating the +> input filter to ensure we get the filter and validator plugin managers +> injected. This allows to use custom registered filters and validators. +> +> Additionally the `InputFilterPluginManager` calls the `init` method _after_ +> instantiating the input filter. ### Register Handler diff --git a/docs/book/application-integration/usage-in-a-zend-mvc-application.md b/docs/book/application-integration/usage-in-a-zend-mvc-application.md index 93b6b412..a72a17c5 100644 --- a/docs/book/application-integration/usage-in-a-zend-mvc-application.md +++ b/docs/book/application-integration/usage-in-a-zend-mvc-application.md @@ -133,9 +133,12 @@ class AlbumControllerFactory implements FactoryInterface > ### Instantiating the InputFilter > -> The `InputFilterPluginManager` calls the `init` method _after_ instantiating -the input filter and injecting it with a factory composing all the various -plugin-manager services. +> The `InputFilterPluginManager` is used instead of directly instantiating the +> input filter to ensure we get the filter and validator plugin managers +> injected. This allows to use custom registered filters and validators. +> +> Additionally the `InputFilterPluginManager` calls the `init` method _after_ +> instantiating the input filter. ### Register Controller From a4271ee4cac2616e2bd6bf4716eddb3b1e40813f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frank=20Bru=CC=88ckner?= Date: Tue, 5 Nov 2019 20:23:00 +0100 Subject: [PATCH 06/14] Fixes spelling of input filter --- .../usage-in-a-zend-expressive-application.md | 6 +++--- .../usage-in-a-zend-mvc-application.md | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/book/application-integration/usage-in-a-zend-expressive-application.md b/docs/book/application-integration/usage-in-a-zend-expressive-application.md index 41e66dbc..c6ffce6d 100644 --- a/docs/book/application-integration/usage-in-a-zend-expressive-application.md +++ b/docs/book/application-integration/usage-in-a-zend-expressive-application.md @@ -7,7 +7,7 @@ plugin manager and a request handler. Before starting, make sure zend-inputfilter is installed and configured. -## Create InputFilter +## Create Input Filter Create an input filter as separate class, e.g. `src/Album/InputFilter/QueryInputFilter.php`: @@ -47,7 +47,7 @@ class QueryInputFilter extends InputFilter } ``` -## Register InputFilter +## Register Input Filter Extend the configuration provider of the module to register the input filter, e.g. `src/Album/ConfigProvider.php`: @@ -81,7 +81,7 @@ class ConfigProvider } ``` -## Using InputFilter +## Using Input Filter ### Create Handler diff --git a/docs/book/application-integration/usage-in-a-zend-mvc-application.md b/docs/book/application-integration/usage-in-a-zend-mvc-application.md index a72a17c5..7b983d8a 100644 --- a/docs/book/application-integration/usage-in-a-zend-mvc-application.md +++ b/docs/book/application-integration/usage-in-a-zend-mvc-application.md @@ -6,9 +6,9 @@ zend-inputfilter plugin manager. Before starting, make sure zend-inputfilter is installed and configured. -## Create Input-Filter +## Create Input Filter -Create an input-filter as separate class, e.g. +Create an input filter as separate class, e.g. `module/Album/src/InputFilter/QueryInputFilter.php`: ```php @@ -67,7 +67,7 @@ return [ ]; ``` -## Using Input-Filter +## Using Input Filter ### Create Controller @@ -131,7 +131,7 @@ class AlbumControllerFactory implements FactoryInterface } ``` -> ### Instantiating the InputFilter +> ### Instantiating the Input Filter > > The `InputFilterPluginManager` is used instead of directly instantiating the > input filter to ensure we get the filter and validator plugin managers From 298fcffeb12e252524e429f71ce39a265c72c216 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frank=20Bru=CC=88ckner?= Date: Tue, 5 Nov 2019 20:27:28 +0100 Subject: [PATCH 07/14] Updates wording for InputFilterPluginManager --- .../usage-in-a-zend-mvc-application.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/docs/book/application-integration/usage-in-a-zend-mvc-application.md b/docs/book/application-integration/usage-in-a-zend-mvc-application.md index 7b983d8a..de168741 100644 --- a/docs/book/application-integration/usage-in-a-zend-mvc-application.md +++ b/docs/book/application-integration/usage-in-a-zend-mvc-application.md @@ -134,11 +134,13 @@ class AlbumControllerFactory implements FactoryInterface > ### Instantiating the Input Filter > > The `InputFilterPluginManager` is used instead of directly instantiating the -> input filter to ensure we get the filter and validator plugin managers -> injected. This allows to use custom registered filters and validators. +> 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. +> instantiating the input filter, ensuring all dependencies are fully injected +> first. ### Register Controller From 16f5cfee557998684ed7d8d3322aeb3adbe5ba46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frank=20Bru=CC=88ckner?= Date: Tue, 5 Nov 2019 20:28:22 +0100 Subject: [PATCH 08/14] Updates wording in introduction in usage for mvc example --- .../usage-in-a-zend-mvc-application.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/book/application-integration/usage-in-a-zend-mvc-application.md b/docs/book/application-integration/usage-in-a-zend-mvc-application.md index de168741..fd70c06c 100644 --- a/docs/book/application-integration/usage-in-a-zend-mvc-application.md +++ b/docs/book/application-integration/usage-in-a-zend-mvc-application.md @@ -1,7 +1,7 @@ # Usage in a zend-mvc Application -The following example shows _one_ usage of zend-inputfilter in a zend-mvc -based application. The example uses a module, a controller and the +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. From ba9a650f4270f8972edf77c988880fac7437382a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frank=20Bru=CC=88ckner?= Date: Tue, 5 Nov 2019 20:29:52 +0100 Subject: [PATCH 09/14] Merges the steps for registering the input filter and controller in mvc example --- .../usage-in-a-zend-mvc-application.md | 37 ++++++------------- 1 file changed, 12 insertions(+), 25 deletions(-) diff --git a/docs/book/application-integration/usage-in-a-zend-mvc-application.md b/docs/book/application-integration/usage-in-a-zend-mvc-application.md index fd70c06c..db96b54e 100644 --- a/docs/book/application-integration/usage-in-a-zend-mvc-application.md +++ b/docs/book/application-integration/usage-in-a-zend-mvc-application.md @@ -46,27 +46,6 @@ class QueryInputFilter extends InputFilter } ``` -## Register Input-Filter - -Extend the configuration of the module to register the input-filter, -e.g. `module/Album/config/module.config.php`: - -```php -namespace Album; - -use Zend\ServiceManager\Factory\InvokableFactory; - -return [ - // Add the following array - 'input_filters' => [ - 'factories => [ - InputFilter\QueryInputFilter::class => InvokableFactory::class, - ], - ], - // … -]; -``` - ## Using Input Filter ### Create Controller @@ -142,21 +121,29 @@ class AlbumControllerFactory implements FactoryInterface > instantiating the input filter, ensuring all dependencies are fully injected > first. -### Register Controller +## Register Input Filter and Controller -Extend the configuration of the module to register the controller, -e.g. `module/Album/config/module.config.php`: +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 [ - // Add the following array 'controllers' => [ 'factories' => [ + // Add this line Controller\AlbumController::class => Controller\AlbumControllerFactory::class, ], ], + // Add the following array + 'input_filters' => [ + 'factories => [ + InputFilter\QueryInputFilter::class => InvokableFactory::class, + ], + ], // … ]; ``` From 05e9e046e6f081f7ee7c3696f873a914cd747dba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frank=20Bru=CC=88ckner?= Date: Tue, 5 Nov 2019 20:34:37 +0100 Subject: [PATCH 10/14] Updates wording for InputFilterPluginManager --- .../usage-in-a-zend-expressive-application.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/docs/book/application-integration/usage-in-a-zend-expressive-application.md b/docs/book/application-integration/usage-in-a-zend-expressive-application.md index c6ffce6d..c48adc67 100644 --- a/docs/book/application-integration/usage-in-a-zend-expressive-application.md +++ b/docs/book/application-integration/usage-in-a-zend-expressive-application.md @@ -146,11 +146,13 @@ class ListHandlerFactory > ### Instantiating the InputFilter > > The `InputFilterPluginManager` is used instead of directly instantiating the -> input filter to ensure we get the filter and validator plugin managers -> injected. This allows to use custom registered filters and validators. +> 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. +> instantiating the input filter, ensuring all dependencies are fully injected +> first. ### Register Handler From 14c75d6b99d6034fe81b394f552c7f782b7a6649 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frank=20Bru=CC=88ckner?= Date: Tue, 5 Nov 2019 20:35:55 +0100 Subject: [PATCH 11/14] Merges the steps for registering the input filter and controller in expressive example --- .../usage-in-a-zend-expressive-application.md | 53 ++++++------------- 1 file changed, 15 insertions(+), 38 deletions(-) diff --git a/docs/book/application-integration/usage-in-a-zend-expressive-application.md b/docs/book/application-integration/usage-in-a-zend-expressive-application.md index c48adc67..db79cc9d 100644 --- a/docs/book/application-integration/usage-in-a-zend-expressive-application.md +++ b/docs/book/application-integration/usage-in-a-zend-expressive-application.md @@ -47,40 +47,6 @@ class QueryInputFilter extends InputFilter } ``` -## Register Input Filter - -Extend the configuration provider of the module to register the input filter, -e.g. `src/Album/ConfigProvider.php`: - -```php -namespace Album; - -use Zend\ServiceManager\Factory\InvokableFactory; - -class ConfigProvider -{ - public function __invoke() : array - { - return [ - 'dependencies' => $this->getDependencies(), - 'input_filters' => $this->getInputFilters(), // <-- Add this line - ]; - } - - // Add the following method - public function getInputFilters() : array - { - return [ - 'factories' => [ - InputFilter\QueryInputFilter::class => InvokableFactory::class, - ], - ]; - } - - // … -} -``` - ## Using Input Filter ### Create Handler @@ -154,10 +120,10 @@ class ListHandlerFactory > instantiating the input filter, ensuring all dependencies are fully injected > first. -### Register Handler +### Register Input Filter and Handler -Extend the configuration provider of the module to register the input filter, -e.g. `src/Album/ConfigProvider.php`: +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; @@ -168,7 +134,7 @@ class ConfigProvider { return [ 'dependencies' => $this->getDependencies(), - 'input_filters' => $this->getInputFilters(), + 'input_filters' => $this->getInputFilters(), // <-- Add this line ]; } @@ -177,6 +143,17 @@ class ConfigProvider 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, ], ]; } From 4bd285f54ecb00134d13c75c76b81254ad88858f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frank=20Bru=CC=88ckner?= Date: Tue, 5 Nov 2019 20:36:31 +0100 Subject: [PATCH 12/14] Updates code examples to included template renderer --- .../usage-in-a-zend-expressive-application.md | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/docs/book/application-integration/usage-in-a-zend-expressive-application.md b/docs/book/application-integration/usage-in-a-zend-expressive-application.md index db79cc9d..ff9775a1 100644 --- a/docs/book/application-integration/usage-in-a-zend-expressive-application.md +++ b/docs/book/application-integration/usage-in-a-zend-expressive-application.md @@ -62,15 +62,22 @@ 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) - { - $this->inputFilter = $inputFilter; + public function __construct( + InputFilterInterface $inputFilter, + TemplateRendererInterface $renderer + ) { + $this->inputFilter = $inputFilter; + $this->renderer = $renderer; } public function handle(ServerRequestInterface $request) : ResponseInterface @@ -80,6 +87,11 @@ class ListHandler implements RequestHandlerInterface $filteredParams = $this->inputFilter->getValues(); // … + + return new HtmlResponse($this->renderer->render( + 'album::list', + [] + )); } } ``` @@ -104,7 +116,10 @@ class ListHandlerFactory $pluginManager = $container->get(InputFilterPluginManager::class); $inputFilter = $pluginManager->get(QueryInputFilter::class); - return new ListHandler($inputFilter); + return new ListHandler( + $inputFilter, + $container->get(TemplateRendererInterface::class) + ); } } ``` From a295e5e627e68495fc37091aa48b80975297090f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frank=20Bru=CC=88ckner?= Date: Tue, 5 Nov 2019 20:38:10 +0100 Subject: [PATCH 13/14] Fixes headline order --- .../usage-in-a-zend-expressive-application.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/book/application-integration/usage-in-a-zend-expressive-application.md b/docs/book/application-integration/usage-in-a-zend-expressive-application.md index ff9775a1..9d697d6d 100644 --- a/docs/book/application-integration/usage-in-a-zend-expressive-application.md +++ b/docs/book/application-integration/usage-in-a-zend-expressive-application.md @@ -135,7 +135,7 @@ class ListHandlerFactory > instantiating the input filter, ensuring all dependencies are fully injected > first. -### Register Input Filter and Handler +## 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`: From b1b560cc41c00514960b28caa8bf4da58ae1c1ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frank=20Bru=CC=88ckner?= Date: Tue, 5 Nov 2019 20:41:39 +0100 Subject: [PATCH 14/14] Updates wording in expressive example for consistency --- .../usage-in-a-zend-expressive-application.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/book/application-integration/usage-in-a-zend-expressive-application.md b/docs/book/application-integration/usage-in-a-zend-expressive-application.md index 9d697d6d..73d6b8b6 100644 --- a/docs/book/application-integration/usage-in-a-zend-expressive-application.md +++ b/docs/book/application-integration/usage-in-a-zend-expressive-application.md @@ -1,9 +1,9 @@ # Usage in a zend-expressive Application -The following example shows _one_ usage of zend-inputfilter in 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. +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.