Will inject the instance with the container and/or router when provided;
otherwise, it will use a ZF2 ServiceManager instance and the FastRoute
router bridge.
The factory also injects the Application with an Emitter\EmitterStack that
composes a SapiEmitter at the bottom of the stack (i.e., will execute last
when the stack is iterated).
public static create ( Interop\Container\ContainerInterface $container = null, Zend\Expressive\Router\RouterInterface $router = null ) : |
||
$container | Interop\Container\ContainerInterface | IoC container from which to fetch middleware defined as services; defaults to a ServiceManager instance |
$router | Zend\Expressive\Router\RouterInterface | Router implementation to use; defaults to the FastRoute router bridge. |
리턴 |
public function testFactoryAllowsPassingRouterToUse() { $router = $this->prophesize('Zend\\Expressive\\Router\\RouterInterface'); $app = AppFactory::create(null, $router->reveal()); $test = $this->getRouterFromApplication($app); $this->assertSame($router->reveal(), $test); }
public function __invoke(ContainerInterface $container) : callable { $middleware = AppFactory::create($container); $middleware->route('/callback', [BodyParamsMiddleware::class, AuthCallback::class], ['GET', 'POST']); $middleware->get('/github', Auth::class); $middleware->get('/google', Auth::class); $middleware->get('/twitter', Auth::class); $middleware->get('/github/oauth2callback', Auth::class); $middleware->get('/google/oauth2callback', Auth::class); $middleware->get('/twitter/oauth2callback', Auth::class); $middleware->get('/logout', Logout::class); $middleware->pipeRoutingMiddleware(); $middleware->pipeDispatchMiddleware(); return $middleware; }
public function __invoke($Services) { $middleware = AppFactory::create($Services); $middleware->route('/callback', [BodyParamsMiddleware::class, Action\AuthCallbackAction::class], ['GET', 'POST']); $config = $Services->get('config'); $strategies = $config['opauth']['Strategy']; // var_dump($strategies);exit('$strategies'); foreach ($strategies as $strategy) { // var_dump($strategy);exit('$strategy'); $middleware->get($strategy['path'], Action\AuthAction::class); $middleware->get($strategy['callback_url'], Action\AuthAction::class); } $middleware->get('/logout', Action\LogoutAction::class); $middleware->pipeRoutingMiddleware(); $middleware->pipeDispatchMiddleware(); return $middleware; }
/** * Tests that requests are passed through the wrapper correctly. */ public function testHandleRequest() { // Create the Expressive app $app = AppFactory::create(); // Add a simple route to the app $app->get('/test', function ($request, $response, $next) { $response->getBody()->write('Hello, World!'); return $response; }); // Create a kernel for the FastCGI daemon using the Expressive app $kernel = new ApplicationWrapper($app); // Create a request to test the route set up for the app $stream = fopen('php://temp', 'r'); $request = new Request(['REQUEST_URI' => '/test'], $stream); // Get the response from the kernel that is wrapping the app $response = $kernel->handleRequest($request); // Check that the app has been wrapper properly by comparing to expected response $this->assertSame('Hello, World!', (string) $response->getBody()); fclose($stream); }
<?php require __DIR__ . '/../vendor/autoload.php'; use Zend\Expressive\AppFactory; use Zend\Stratigility\Next; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; $app = AppFactory::create(); $app->get('/', function (RequestInterface $request, ResponseInterface $response, Next $next) { $response->getBody()->write('Hello!'); return $response; }); $app->run();
public function __invoke(ContainerInterface $container) { return AppFactory::create($container); }