Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Example in documentation throws a fatal error #100

Closed
patrick-fls opened this issue Mar 7, 2017 · 7 comments
Closed

Example in documentation throws a fatal error #100

patrick-fls opened this issue Mar 7, 2017 · 7 comments
Assignees

Comments

@patrick-fls
Copy link

patrick-fls commented Mar 7, 2017

Using 2.0.1
https://zendframework.github.io/zend-stratigility/middleware/

Will throw

Fatal error: Uncaught Zend\Stratigility\Exception\MissingResponsePrototypeException:
Cannot wrap callable middleware; no Zend\Stratigility\Middleware\CallableMiddlewareWrapperFactory
or Psr\Http\Message\ResponseInterface instances composed in middleware pipeline; use
setCallableMiddlewareDecorator() or setResponsePrototype() on your Zend\Stratigility\MiddlewarePipe
instance to provide one or the other, or decorate callable middleware manually before piping.
@weierophinney
Copy link
Member

Which example, specifically?

Also, the exception details what you need to do; what we'll be doing is updating the example to add the response prototype, but first we need to know which example is leading to the error you're seeing.

@patrick-fls
Copy link
Author

this one:

use Zend\Stratigility\MiddlewarePipe;
use Zend\Diactoros\Server;

require __DIR__ . '/../vendor/autoload.php';

$app    = new MiddlewarePipe();
$server = Server::createServer($app, $_SERVER, $_GET, $_POST, $_COOKIE, $_FILES);

// Landing page
$app->pipe('/', function ($req, $res, $next) {
    if (! in_array($req->getUri()->getPath(), ['/', ''], true)) {
        return $next($req, $res);
    }
    $res->getBody()->write('Hello world!');
    return $res;
});

// Another page
$app->pipe('/foo', function ($req, $res, $next) {
    $res->getBody()->write('FOO!');
    return $res;
});

$server->listen();

@geerteltink
Copy link
Member

geerteltink commented Mar 8, 2017

How about this:

<?php

use Zend\Stratigility\MiddlewarePipe;
use Zend\Stratigility\NoopFinalHandler;
use Zend\Diactoros\Server;

require __DIR__ . '/vendor/autoload.php';

$app    = new MiddlewarePipe();
$app->setResponsePrototype(new \Zend\Diactoros\Response());

$server = Server::createServer($app, $_SERVER, $_GET, $_POST, $_COOKIE, $_FILES);

// Landing page
$app->pipe('/', function ($req, $res, $next) {
    if (! in_array($req->getUri()->getPath(), ['/', ''], true)) {
        return $next($req, $res);
    }
    $res->getBody()->write('Hello world!');
    return $res;
});

// Another page
$app->pipe('/foo', function ($req, $res, $next) {
    $res->getBody()->write('FOO!');
    return $res;
});

$server->listen(new NoopFinalHandler());

Original example code is here: https://github.com/zendframework/zend-stratigility/blob/master/doc/book/middleware.md

@patrick-fls
Copy link
Author

Yep that works!

weierophinney added a commit that referenced this issue Mar 9, 2017
As reported in #100, the example in the middleware page fails currently,
as piping callable double-pass middleware requires a response prototype
be composed in the pipeline. This patch adds that.
weierophinney added a commit that referenced this issue Mar 9, 2017
@weierophinney weierophinney self-assigned this Mar 9, 2017
@weierophinney
Copy link
Member

Fixed with 2c18525. Thanks for the report!

@geerteltink
Copy link
Member

geerteltink commented Mar 9, 2017

@weierophinney That fixed the first part. You still need to supply a final handler to the listener as in my example: $server->listen(new NoopFinalHandler());. Without it you will get this error:

Uncaught TypeError: Argument 2 passed to Zend\Stratigility\MiddlewarePipe::process() must be an instance of Interop\Http\ServerMiddleware\DelegateInterface, null given ...

weierophinney added a commit that referenced this issue Mar 9, 2017
weierophinney added a commit that referenced this issue Mar 9, 2017
@weierophinney
Copy link
Member

Thanks, @xtreamwayz — updated now with that.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants