Php Guzzle 5中的Setter不允许在Guzzle 6中使用
我正在使用Guzzle对我在Symfony2中开发的API进行测试 我一直在看knpUniversity的Rest系列,我在更新应该能够使用app_test.php而不是app.php的部分时遇到了问题。 在该系列中,他们使用Guzzle v5,但在v6中有一些重大变化 在本教程中,要允许使用以app.php以外的其他uri,请执行以下操作: 但是对于Guzzle6,没有用于请求的setter:Php Guzzle 5中的Setter不允许在Guzzle 6中使用,php,symfony,guzzle,guzzle6,Php,Symfony,Guzzle,Guzzle6,我正在使用Guzzle对我在Symfony2中开发的API进行测试 我一直在看knpUniversity的Rest系列,我在更新应该能够使用app_test.php而不是app.php的部分时遇到了问题。 在该系列中,他们使用Guzzle v5,但在v6中有一些重大变化 在本教程中,要允许使用以app.php以外的其他uri,请执行以下操作: 但是对于Guzzle6,没有用于请求的setter: use GuzzleHttp\Client; use GuzzleHttp\HandlerStack
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
class ApiTestCase extends KernelTestCase
{
private static $staticClient;
protected $client;
public static function setUpBeforeClass()
{
//defined in phpunit.xml
$baseUri = getenv('TEST_BASE_URI');
//Create a handler stack that has all of the default middlewares attached
$handler = HandlerStack::create();
// guaranteeing that /app_test.php is prefixed to all URLs
$handler->push(Middleware::mapRequest(function (RequestInterface $request) {
$path = $request->getUri()->getPath();
if (strpos($path, '/api') === 0) {
$request->getUri()->setPath('/app_test.php'.$path); // <-- this is not allowed
}
// Notice that we have to return a request object
return $request->withHeader('X-Foo', 'Bar');
}));
// Inject the handler into the client
self::$staticClient = new Client([
'base_uri' => $baseUri.'/app_test.php',
'handler' => $handler,
'defaults' => [
'http_errors' => false
]
]);
//Allows to use service container
self::bootKernel();
}
谢谢@Cerad我想像$request=$request->getUri()->withPath()这样的东西会有用。可能需要获取uri对象,然后使用$request->withUri()。`$handler->push(中间件::mapRequest(函数(RequestInterface$request){$path=$request->getUri()->getPath();if(strpos($path,'/api/v1')==0){$uri=$request->getUri()->withPath('/app_test.php.$path);$request->withUri($uri);}var_dump($request->getUri()->getPath());return$request->withHeader('X-Foo','Bar');});`仍然显示没有前缀的uri。我认为像$request=$request->getUri()->withPath()这样的东西可以工作。可能需要获取uri对象,然后使用$request->withUri()。`$handler->push(中间件::mapRequest(函数(RequestInterface$request){$path=$request->getUri()->getPath();if(strpos($path,'/api/v1')==0){$uri=$request->getUri()->withPath('/app_test.php.$path);$request->withUri($uri);}var_dump($request->getUri()->getPath());return$request->withHeader('X-Foo','Bar');});`仍然显示不带前缀的uri。
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
class ApiTestCase extends KernelTestCase
{
private static $staticClient;
protected $client;
public static function setUpBeforeClass()
{
//defined in phpunit.xml
$baseUri = getenv('TEST_BASE_URI');
//Create a handler stack that has all of the default middlewares attached
$handler = HandlerStack::create();
// guaranteeing that /app_test.php is prefixed to all URLs
$handler->push(Middleware::mapRequest(function (RequestInterface $request) {
$path = $request->getUri()->getPath();
if (strpos($path, '/api') === 0) {
$request->getUri()->setPath('/app_test.php'.$path); // <-- this is not allowed
}
// Notice that we have to return a request object
return $request->withHeader('X-Foo', 'Bar');
}));
// Inject the handler into the client
self::$staticClient = new Client([
'base_uri' => $baseUri.'/app_test.php',
'handler' => $handler,
'defaults' => [
'http_errors' => false
]
]);
//Allows to use service container
self::bootKernel();
}
$handler->push(Middleware::mapRequest(function(RequestInterface $request) {
$path = $request->getUri()->getPath();
if (strpos($path, '/app_test.php') !== 0) {
$path = '/app_test.php' . $path;
}
$uri = $request->getUri()->withPath($path);
return $request->withUri($uri);
}));