From e3b16644dd065a59cc230c1bf06c44c6578531ce Mon Sep 17 00:00:00 2001 From: Adrian Date: Tue, 25 Oct 2016 09:37:03 +0200 Subject: [PATCH 1/3] Move config file --- config/{autoload/full.global.php => config.php} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename config/{autoload/full.global.php => config.php} (100%) diff --git a/config/autoload/full.global.php b/config/config.php similarity index 100% rename from config/autoload/full.global.php rename to config/config.php From 3cbbc0cf243eea9b1d2938b341b1217758dbc229 Mon Sep 17 00:00:00 2001 From: Adrian Date: Tue, 25 Oct 2016 09:38:41 +0200 Subject: [PATCH 2/3] Use seperate container script --- config/config.php | 4 ++-- config/container.php | 13 +++++++++++++ public/index.php | 10 ++-------- 3 files changed, 17 insertions(+), 10 deletions(-) create mode 100644 config/container.php diff --git a/config/config.php b/config/config.php index 00c071f..fe9dc43 100644 --- a/config/config.php +++ b/config/config.php @@ -1,4 +1,4 @@ - [ Zend\Expressive\Container\ApplicationFactory::ROUTING_MIDDLEWARE, Zend\Expressive\Container\ApplicationFactory::DISPATCH_MIDDLEWARE, @@ -10,4 +10,4 @@ 'allowed_methods' => [ 'GET' ], ], ], -]; +]); diff --git a/config/container.php b/config/container.php new file mode 100644 index 0000000..33cd86c --- /dev/null +++ b/config/container.php @@ -0,0 +1,13 @@ +get(Application::class); $app->run(); From 6aa311b35d855ae62f591ca0dbf9b4c982d34ba7 Mon Sep 17 00:00:00 2001 From: Adrian Date: Tue, 25 Oct 2016 10:48:15 +0200 Subject: [PATCH 3/3] Add UrlHelper The helper can be used to generate URLs from named routes. HomeAction was converted to a service to inject the URL helper using a factory. Also see: [Container PSR and the Service Locator](https://github.com/php-fig/fig-standards/blob/master/proposed/container-meta.md#4-recommended-usage-container-psr-and-the-service-locator) --- composer.json | 3 ++- config/config.php | 10 +++++++++- config/container.php | 12 ++++++++++++ src/App/HomeAction.php | 11 ++++++++++- src/App/HomeFactory.php | 19 +++++++++++++++++++ 5 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 src/App/HomeFactory.php diff --git a/composer.json b/composer.json index b518a01..c2f3f31 100644 --- a/composer.json +++ b/composer.json @@ -7,6 +7,7 @@ "require": { "xtreamwayz/pimple-container-interop": "^1.0", "zendframework/zend-expressive": "^1.0", - "zendframework/zend-expressive-fastroute": "^1.2" + "zendframework/zend-expressive-fastroute": "^1.2", + "zendframework/zend-expressive-helpers": "^2.1" } } diff --git a/config/config.php b/config/config.php index fe9dc43..754627a 100644 --- a/config/config.php +++ b/config/config.php @@ -1,13 +1,21 @@ [ Zend\Expressive\Container\ApplicationFactory::ROUTING_MIDDLEWARE, + ['middleware' => Zend\Expressive\Helper\UrlHelperMiddleware::class], Zend\Expressive\Container\ApplicationFactory::DISPATCH_MIDDLEWARE, ], 'routes' => [ [ 'path' => '/', - 'middleware' => new Test\App\HomeAction, + 'middleware' => Test\App\HomeAction::class, 'allowed_methods' => [ 'GET' ], + 'name' => 'home', + ], + [ + 'path' => '/test/another/path', + 'middleware' => Test\App\HomeAction::class, + 'allowed_methods' => [ 'GET' ], + 'name' => 'other', ], ], ]); diff --git a/config/container.php b/config/container.php index 33cd86c..c516330 100644 --- a/config/container.php +++ b/config/container.php @@ -1,13 +1,25 @@ urlhelper = $urlhelper; + } + public function __invoke(Request $req, Response $res, callable $next) { - $res->getBody()->write("Hello World!"); + $result = sprintf("Hello World!
\n%s", var_export($this->urlhelper->generate('other'), true)); + $res->getBody()->write($result); } } diff --git a/src/App/HomeFactory.php b/src/App/HomeFactory.php new file mode 100644 index 0000000..33ea90e --- /dev/null +++ b/src/App/HomeFactory.php @@ -0,0 +1,19 @@ +get(UrlHelper::class); + } catch (\Exception $e) { + trigger_error("Ex: $e"); + } + return new HomeAction($urlhelper); + } +}