Compare commits

...

3 Commits

Author SHA1 Message Date
Adrian 6aa311b35d 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)
2016-10-25 10:48:15 +02:00
Adrian 3cbbc0cf24 Use seperate container script 2016-10-25 09:38:41 +02:00
Adrian e3b16644dd Move config file 2016-10-25 09:37:03 +02:00
7 changed files with 79 additions and 23 deletions

View File

@ -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"
}
}

View File

@ -1,13 +0,0 @@
<?php return [
'middleware_pipeline' => [
Zend\Expressive\Container\ApplicationFactory::ROUTING_MIDDLEWARE,
Zend\Expressive\Container\ApplicationFactory::DISPATCH_MIDDLEWARE,
],
'routes' => [
[
'path' => '/',
'middleware' => new Test\App\HomeAction,
'allowed_methods' => [ 'GET' ],
],
],
];

21
config/config.php 100644
View File

@ -0,0 +1,21 @@
<?php return new ArrayObject([
'middleware_pipeline' => [
Zend\Expressive\Container\ApplicationFactory::ROUTING_MIDDLEWARE,
['middleware' => Zend\Expressive\Helper\UrlHelperMiddleware::class],
Zend\Expressive\Container\ApplicationFactory::DISPATCH_MIDDLEWARE,
],
'routes' => [
[
'path' => '/',
'middleware' => Test\App\HomeAction::class,
'allowed_methods' => [ 'GET' ],
'name' => 'home',
],
[
'path' => '/test/another/path',
'middleware' => Test\App\HomeAction::class,
'allowed_methods' => [ 'GET' ],
'name' => 'other',
],
],
]);

View File

@ -0,0 +1,25 @@
<?php
use Test\App\HomeAction;
use Test\App\HomeFactory;
use Xtreamwayz\Pimple\Container;
use Zend\Expressive\Application;
use Zend\Expressive\Container\ApplicationFactory;
use Zend\Expressive\Helper\UrlHelper;
use Zend\Expressive\Helper\UrlHelperFactory;
use Zend\Expressive\Helper\UrlHelperMiddleware;
use Zend\Expressive\Helper\UrlHelperMiddlewareFactory;
use Zend\Expressive\Router\FastRouteRouter;
use Zend\Expressive\Router\RouterInterface;
$c = new Container;
$c['config'] = require 'config/config.php';
$c[RouterInterface::class] = function () { return new FastRouteRouter; };
$c[UrlHelper::class] = new UrlHelperFactory;
$c[UrlHelperMiddleware::class] = new UrlHelperMiddlewareFactory;
$c[HomeAction::class] = new HomeFactory;
$c[Application::class] = new ApplicationFactory;
return $c;

View File

@ -1,18 +1,12 @@
<?php
use Xtreamwayz\Pimple\Container;
use Zend\Expressive\Application;
use Zend\Expressive\Container\ApplicationFactory;
chdir(dirname(__DIR__));
require_once 'vendor/autoload.php';
$c = new Container;
$c = require 'config/container.php';
$c['config'] = new ArrayObject(require 'config/autoload/full.global.php');
$c[Application::class] = new ApplicationFactory;
$app = $c[Application::class];
$app = $c->get(Application::class);
$app->run();

View File

@ -4,11 +4,20 @@ namespace Test\App;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Zend\Expressive\Helper\UrlHelper;
class HomeAction
{
private $urlhelper;
public function __construct(UrlHelper $urlhelper)
{
$this->urlhelper = $urlhelper;
}
public function __invoke(Request $req, Response $res, callable $next)
{
$res->getBody()->write("Hello World!");
$result = sprintf("Hello World!<br/>\n%s", var_export($this->urlhelper->generate('other'), true));
$res->getBody()->write($result);
}
}

View File

@ -0,0 +1,19 @@
<?php
namespace Test\App;
use Interop\Container\ContainerInterface;
use Zend\Expressive\Helper\UrlHelper;
class HomeFactory
{
public function __invoke(ContainerInterface $c)
{
try {
$urlhelper = $c->get(UrlHelper::class);
} catch (\Exception $e) {
trigger_error("Ex: $e");
}
return new HomeAction($urlhelper);
}
}