This factory acts as the general entry point for using Application in a
programmatic vs service-driven environment.
The Application instance returned is guaranteed to have a router, a
container, and an emitter stack; by default, the FastRoute router and the
ZF2 ServiceManager are used.
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); }