PHP Class Zend\Expressive\Application

Inheritance: extends Zend\Stratigility\MiddlewarePipe, implements Zend\Expressive\Router\RouteResultSubjectInterface, use trait MarshalMiddlewareTrait
Show file Open project: zendframework/zend-expressive Class Usage Examples

Public Methods

Method Description
__call ( string $method, array $args ) : Zend\Expressive\Router\Route
__construct ( Zend\Expressive\Router\RouterInterface $router, Interop\Container\ContainerInterface $container = null, callable $finalHandler = null, Zend\Diactoros\Response\EmitterInterface $emitter = null ) Constructor
__invoke ( Psr\Http\Message\ServerRequestInterface $request, Psr\Http\Message\ResponseInterface $response, callable $out = null ) : Psr\Http\Message\ResponseInterface Overload middleware invocation.
any ( string | Zend\Expressive\Router\Route $path, callable | string $middleware, null | string $name = null ) : Zend\Expressive\Router\Route
attachRouteResultObserver ( Zend\Expressive\Router\RouteResultObserverInterface $observer ) Attach a route result observer.
detachRouteResultObserver ( Zend\Expressive\Router\RouteResultObserverInterface $observer ) Detach a route result observer.
dispatchMiddleware ( Psr\Http\Message\ServerRequestInterface $request, Psr\Http\Message\ResponseInterface $response, callable $next ) Dispatch the middleware matched by routing.
getContainer ( ) : Interop\Container\ContainerInterface Retrieve the IoC container.
getEmitter ( ) : Zend\Diactoros\Response\EmitterInterface Retrieve an emitter to use during run().
getFinalHandler ( Psr\Http\Message\ResponseInterface $response = null ) : callable | null Return the final handler to use during run() if the stack is exhausted.
notifyRouteResultObservers ( Zend\Expressive\Router\RouteResult $result ) Notify all route result observers with the given route result.
pipe ( string | array | callable $path, null | string | array | callable $middleware = null ) : self Overload pipe() operation.
pipeDispatchMiddleware ( ) Register the dispatch middleware in the middleware pipeline.
pipeErrorHandler ( string | callable $path, null | string | callable $middleware = null ) : self Pipe an error handler.
pipeRouteResultObserverMiddleware ( ) Register the route result observer middleware in the middleware pipeline.
pipeRoutingMiddleware ( ) Register the routing middleware in the middleware pipeline.
route ( string | Zend\Expressive\Router\Route $path, callable | string | array $middleware = null, array $methods = null, null | string $name = null ) : Zend\Expressive\Router\Route Add a route for the route middleware to match.
routeMiddleware ( Psr\Http\Message\ServerRequestInterface $request, Psr\Http\Message\ResponseInterface $response, callable $next ) : Psr\Http\Message\ResponseInterface Middleware that routes the incoming request and delegates to the matched middleware.
routeResultObserverMiddleware ( Psr\Http\Message\ServerRequestInterface $request, Psr\Http\Message\ResponseInterface $response, callable $next ) Middleware for notifying route result observers.
run ( Psr\Http\Message\ServerRequestInterface $request = null, Psr\Http\Message\ResponseInterface $response = null ) Run the application

Private Methods

Method Description
checkForDuplicateRoute ( string $path, null | array $methods = null ) Determine if the route is duplicated in the current list.

Method Details

__call() public method

public __call ( string $method, array $args ) : Zend\Expressive\Router\Route
$method string
$args array
return Zend\Expressive\Router\Route

__construct() public method

Calls on the parent constructor, and then uses the provided arguments to set internal properties.
public __construct ( Zend\Expressive\Router\RouterInterface $router, Interop\Container\ContainerInterface $container = null, callable $finalHandler = null, Zend\Diactoros\Response\EmitterInterface $emitter = null )
$router Zend\Expressive\Router\RouterInterface
$container Interop\Container\ContainerInterface IoC container from which to pull services, if any.
$finalHandler callable Final handler to use when $out is not provided on invocation.
$emitter Zend\Diactoros\Response\EmitterInterface Emitter to use when `run()` is invoked.

__invoke() public method

If $out is not provided, uses the result of getFinalHandler().
public __invoke ( Psr\Http\Message\ServerRequestInterface $request, Psr\Http\Message\ResponseInterface $response, callable $out = null ) : Psr\Http\Message\ResponseInterface
$request Psr\Http\Message\ServerRequestInterface
$response Psr\Http\Message\ResponseInterface
$out callable
return Psr\Http\Message\ResponseInterface

any() public method

public any ( string | Zend\Expressive\Router\Route $path, callable | string $middleware, null | string $name = null ) : Zend\Expressive\Router\Route
$path string | Zend\Expressive\Router\Route
$middleware callable | string Middleware (or middleware service name) to associate with route.
$name null | string the name of the route
return Zend\Expressive\Router\Route

attachRouteResultObserver() public method

Attach a route result observer.
Deprecation: This method will be removed in v1.1.
public attachRouteResultObserver ( Zend\Expressive\Router\RouteResultObserverInterface $observer )
$observer Zend\Expressive\Router\RouteResultObserverInterface

detachRouteResultObserver() public method

Detach a route result observer.
Deprecation: This method will be removed in v1.1.
public detachRouteResultObserver ( Zend\Expressive\Router\RouteResultObserverInterface $observer )
$observer Zend\Expressive\Router\RouteResultObserverInterface

dispatchMiddleware() public method

If the request does not have the route result, calls on the next middleware. Next, it checks if the route result has matched middleware; if not, it raises an exception. Finally, it attempts to marshal the middleware, and dispatches it when complete, return the response.
public dispatchMiddleware ( Psr\Http\Message\ServerRequestInterface $request, Psr\Http\Message\ResponseInterface $response, callable $next )
$request Psr\Http\Message\ServerRequestInterface
$response Psr\Http\Message\ResponseInterface
$next callable

getContainer() public method

If no IoC container is registered, we raise an exception.
public getContainer ( ) : Interop\Container\ContainerInterface
return Interop\Container\ContainerInterface

getEmitter() public method

If none was registered during instantiation, this will lazy-load an EmitterStack composing an SapiEmitter instance.
public getEmitter ( ) : Zend\Diactoros\Response\EmitterInterface
return Zend\Diactoros\Response\EmitterInterface

getFinalHandler() public method

Return the final handler to use during run() if the stack is exhausted.
public getFinalHandler ( Psr\Http\Message\ResponseInterface $response = null ) : callable | null
$response Psr\Http\Message\ResponseInterface Response instance with which to seed the FinalHandler; used to determine if the response passed to the handler represents the original or final response state.
return callable | null

notifyRouteResultObservers() public method

Notify all route result observers with the given route result.
Deprecation: This method will be removed in v1.1.
public notifyRouteResultObservers ( Zend\Expressive\Router\RouteResult $result )
$result Zend\Expressive\Router\RouteResult

pipe() public method

Middleware piped may be either callables or service names. Middleware specified as services will be wrapped in a closure similar to the following: function ($request, $response, $next = null) use ($container, $middleware) { $invokable = $container->get($middleware); if (! is_callable($invokable)) { throw new Exception\InvalidMiddlewareException(sprintf( 'Lazy-loaded middleware "%s" is not invokable', $middleware )); } return $invokable($request, $response, $next); }; This is done to delay fetching the middleware until it is actually used; the upshot is that you will not be notified if the service is invalid to use as middleware until runtime. Middleware may also be passed as an array; each item in the array must resolve to middleware eventually (i.e., callable or service name). Finally, ensures that the route middleware is only ever registered once.
public pipe ( string | array | callable $path, null | string | array | callable $middleware = null ) : self
$path string | array | callable Either a URI path prefix, or middleware.
$middleware null | string | array | callable Middleware
return self

pipeDispatchMiddleware() public method

Register the dispatch middleware in the middleware pipeline.

pipeErrorHandler() public method

Middleware piped may be either callables or service names. Middleware specified as services will be wrapped in a closure similar to the following: function ($error, $request, $response, $next) use ($container, $middleware) { $invokable = $container->get($middleware); if (! is_callable($invokable)) { throw new Exception\InvalidMiddlewareException(sprintf( 'Lazy-loaded middleware "%s" is not invokable', $middleware )); } return $invokable($error, $request, $response, $next); }; This is done to delay fetching the middleware until it is actually used; the upshot is that you will not be notified if the service is invalid to use as middleware until runtime. Once middleware detection and wrapping (if necessary) is complete, proxies to pipe().
public pipeErrorHandler ( string | callable $path, null | string | callable $middleware = null ) : self
$path string | callable Either a URI path prefix, or middleware.
$middleware null | string | callable Middleware
return self

pipeRouteResultObserverMiddleware() public method

Register the route result observer middleware in the middleware pipeline.
Deprecation: This method will be removed in v1.1.

pipeRoutingMiddleware() public method

Register the routing middleware in the middleware pipeline.

route() public method

Accepts either a Router\Route instance, or a combination of a path and middleware, and optionally the HTTP methods allowed. On first invocation, pipes the route middleware to the middleware pipeline.
public route ( string | Zend\Expressive\Router\Route $path, callable | string | array $middleware = null, array $methods = null, null | string $name = null ) : Zend\Expressive\Router\Route
$path string | Zend\Expressive\Router\Route
$middleware callable | string | array Middleware (or middleware service name) to associate with route.
$methods array HTTP method to accept; null indicates any.
$name null | string the name of the route
return Zend\Expressive\Router\Route

routeMiddleware() public method

Uses the router to route the incoming request, injecting the request with: - the route result object (under a key named for the RouteResult class) - attributes for each matched routing parameter On completion, it calls on the next middleware (typically the dispatchMiddleware()). If routing fails, $next() is called; if routing fails due to HTTP method negotiation, the response is set to a 405, injected with an Allow header, and $next() is called with its $error argument set to the value 405 (invoking the next error middleware).
public routeMiddleware ( Psr\Http\Message\ServerRequestInterface $request, Psr\Http\Message\ResponseInterface $response, callable $next ) : Psr\Http\Message\ResponseInterface
$request Psr\Http\Message\ServerRequestInterface
$response Psr\Http\Message\ResponseInterface
$next callable
return Psr\Http\Message\ResponseInterface

routeResultObserverMiddleware() public method

If the request has a route result, calls notifyRouteResultObservers(). This middleware should be injected between the routing and dispatch middleware when creating your middleware pipeline. If you are using this, rewrite your observers as middleware that pulls the route result from the request instead.
Deprecation: This method will be removed in v1.1.
public routeResultObserverMiddleware ( Psr\Http\Message\ServerRequestInterface $request, Psr\Http\Message\ResponseInterface $response, callable $next )
$request Psr\Http\Message\ServerRequestInterface
$response Psr\Http\Message\ResponseInterface
$next callable

run() public method

If no request or response are provided, the method will use ServerRequestFactory::fromGlobals to create a request instance, and instantiate a default response instance. It then will invoke itself with the request and response, and emit the returned response using the composed emitter.
public run ( Psr\Http\Message\ServerRequestInterface $request = null, Psr\Http\Message\ResponseInterface $response = null )
$request Psr\Http\Message\ServerRequestInterface
$response Psr\Http\Message\ResponseInterface