本文整理汇总了PHP中Slim\Http\Response::write方法的典型用法代码示例。如果您正苦于以下问题:PHP Response::write方法的具体用法?PHP Response::write怎么用?PHP Response::write使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Slim\Http\Response
的用法示例。
在下文中一共展示了Response::write方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: __invoke
public function __invoke(Request $request, Response $response, $arguments)
{
$response->write('this is just the beginning of my application');
foreach ($arguments as $argument) {
$response->write("\n {$argument}");
}
}
示例2: render
public function render(Response $response, $template = "", $data = [])
{
extract($data);
ob_start();
include $this->templatePath . $template;
$output = ob_get_clean();
return $response->write($output);
}
示例3: __invoke
/**
* @param \swoole_http_request $request
* @param \swoole_http_response $response
* @throws \Exception
*/
public function __invoke($request, $response)
{
$this->app->getContainer()['environment'] = $this->app->getContainer()->factory(function () {
return new Environment($_SERVER);
});
$this->app->getContainer()['request'] = $this->app->getContainer()->factory(function ($container) {
return Request::createFromEnvironment($container['environment']);
});
$this->app->getContainer()['response'] = $this->app->getContainer()->factory(function ($container) {
$headers = new Headers(['Content-Type' => 'text/html']);
$response = new Response(200, $headers);
return $response->withProtocolVersion($container->get('settings')['httpVersion']);
});
/**
* @var ResponseInterface $appResponse
*/
$appResponse = $this->app->run(true);
// set http header
foreach ($appResponse->getHeaders() as $key => $value) {
$filter_header = function ($header) {
$filtered = str_replace('-', ' ', $header);
$filtered = ucwords($filtered);
return str_replace(' ', '-', $filtered);
};
$name = $filter_header($key);
foreach ($value as $v) {
$response->header($name, $v);
}
}
// set http status
$response->status($appResponse->getStatusCode());
// send response to browser
if (!$this->isEmptyResponse($appResponse)) {
$body = $appResponse->getBody();
if ($body->isSeekable()) {
$body->rewind();
}
$settings = $this->app->getContainer()->get('settings');
$chunkSize = $settings['responseChunkSize'];
$contentLength = $appResponse->getHeaderLine('Content-Length');
if (!$contentLength) {
$contentLength = $body->getSize();
}
$totalChunks = ceil($contentLength / $chunkSize);
$lastChunkSize = $contentLength % $chunkSize;
$currentChunk = 0;
while (!$body->eof() && $currentChunk < $totalChunks) {
if (++$currentChunk == $totalChunks && $lastChunkSize > 0) {
$chunkSize = $lastChunkSize;
}
$response->write($body->read($chunkSize));
if (connection_status() != CONNECTION_NORMAL) {
break;
}
}
$response->end();
}
}
示例4: error
/**
* Error Handler
*
* This method defines or invokes the application-wide Error handler.
* There are two contexts in which this method may be invoked:
*
* 1. When declaring the handler:
*
* If the $argument parameter is callable, this
* method will register the callable to be invoked when an uncaught
* Exception is detected, or when otherwise explicitly invoked.
* The handler WILL NOT be invoked in this context.
*
* 2. When invoking the handler:
*
* If the $argument parameter is not callable, Slim assumes you want
* to invoke an already-registered handler. If the handler has been
* registered and is callable, it is invoked and passed the caught Exception
* as its one and only argument. The error handler's output is captured
* into an output buffer and sent as the body of a 500 HTTP Response.
*
* @param mixed $argument Callable|\Exception
*/
public function error($argument = null)
{
if (is_callable($argument)) {
//Register error handler
$this->router->error($argument);
} else {
//Invoke error handler
$this->response->status(500);
$this->response->body('');
$this->response->write($this->callErrorHandler($argument));
$this->stop();
}
}
示例5: __invoke
public function __invoke(Request $request, Response $response)
{
$data = ['uri' => $request->getUri()->getPath(), 'contact' => $this->container->router->pathFor('contact')];
// We need a new response body
$headers = new Headers(['Content-Type' => 'text/html; charset=UTF-8']);
$res = new Response(404, $headers);
// Start buffering
ob_start();
$view = new View($res);
$view->setTemplate('errors.404');
$view->render($data);
// Get the buffer output
$content = ob_get_clean();
return $res->write($content);
}
示例6: __invoke
/**
* __invoke magical method
*
* @param ServerRequestInterface $request A valid PSR-7 Request object that inherits the `ServerRequestInterface` interface
* @param ResponseInterface $response A valid PSR-7 Response object that inherits `ResponseInterface` interface
* @param Exception $exception Exception to be handled
* @return ResponseInterface object
*/
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Exception $exception)
{
// We need a new response body
$headers = new Headers(['Content-Type' => 'text/html; charset=UTF-8']);
$res = new Response(500, $headers);
$handler = new Handler($exception);
// Start buffering
ob_start();
// Create a new View instance just for this page
$view = new View($res);
$view->setTemplate('errors.exhan');
$view->render($handler->getExceptionData());
// Get the buffer output
$content = ob_get_clean();
return $res->write($content);
}
示例7: testGetRequest
public function testGetRequest()
{
$request = m::mock('\\swoole_http_request');
$response = m::mock('\\swoole_http_response');
$container = new Container();
$app = m::mock(App::class);
$handler = new RequestHandler($app);
$mockResponse = new Response(200, new Headers(['Content-Type' => 'text/html', 'Content-Length' => 5]));
$mockResponse->write('hello');
$app->shouldReceive('getContainer')->andReturn($container);
$app->shouldReceive('run')->once()->andReturn($mockResponse);
$response->shouldReceive('header')->twice();
$response->shouldReceive('status')->once()->withArgs([200]);
$response->shouldReceive('write')->once()->withArgs(['hello']);
$response->shouldReceive('end')->once();
$handler($request, $response);
}
示例8: response
/**
* We wrap the base
* @param Response $response
* @param array $data
* @param bool $merge
* @return Response
*/
public function response(Response $response, $data = [], $merge = false)
{
return $response->write($this->render($data, $merge));
}
示例9: testInvokeWithPimpleCallable
public function testInvokeWithPimpleCallable()
{
// Prepare request and response objects
$env = Environment::mock(['SCRIPT_NAME' => '/index.php', 'REQUEST_URI' => '/foo', 'REQUEST_METHOD' => 'GET']);
$uri = Uri::createFromEnvironment($env);
$headers = Headers::createFromEnvironment($env);
$cookies = [];
$serverParams = $env->all();
$body = new RequestBody();
$req = new Request('GET', $uri, $headers, $cookies, $serverParams, $body);
$res = new Response();
$mock = $this->getMock('StdClass', ['bar']);
$app = new App();
$container = $app->getContainer();
$container['foo'] = function () use($mock, $res) {
$mock->method('bar')->willReturn($res->write('Hello'));
return $mock;
};
$app->get('/foo', 'foo:bar');
// Invoke app
$resOut = $app($req, $res);
$this->assertInstanceOf('\\Psr\\Http\\Message\\ResponseInterface', $resOut);
$this->assertEquals('Hello', (string) $res->getBody());
}
示例10: testFinalize
public function testFinalize()
{
$method = new \ReflectionMethod('Slim\\App', 'finalize');
$method->setAccessible(true);
$response = new Response();
$response->write('foo');
$response = $method->invoke(new App(), $response);
$this->assertTrue($response->hasHeader('Content-Length'));
$this->assertEquals('3', $response->getHeaderLine('Content-Length'));
}
示例11: dispatchRequest
/**
* Dispatch request and build response
*
* This method will route the provided Request object against all available
* application routes. The provided response will reflect the status, header, and body
* set by the invoked matching route.
*
* The provided Request and Response objects are updated by reference. There is no
* value returned by this method.
*
* @param \Slim\Http\Request The request instance
* @param \Slim\Http\Response The response instance
*/
protected function dispatchRequest(\Slim\Http\Request $request, \Slim\Http\Response $response)
{
try {
$this->applyHook('slim.before');
ob_start();
$this->applyHook('slim.before.router');
$dispatched = false;
$matchedRoutes = $this['router']->getMatchedRoutes($request->getMethod(), $request->getPathInfo(), false);
foreach ($matchedRoutes as $route) {
try {
$this->applyHook('slim.before.dispatch');
$dispatched = $route->dispatch();
$this->applyHook('slim.after.dispatch');
if ($dispatched) {
break;
}
} catch (\Slim\Exception\Pass $e) {
continue;
}
}
if (!$dispatched) {
$this->notFound();
}
$this->applyHook('slim.after.router');
} catch (\Slim\Exception\Stop $e) {
}
$response->write(ob_get_clean());
$this->applyHook('slim.after');
}
示例12: testIpAllow
/**
* Integration test IpRestrictMiddleware::_invoke() when the given IP is allowed.
*/
public function testIpAllow()
{
// Prepare the Request and the application.
$app = new App();
// Setup a demo environment
$env = Environment::mock(['SCRIPT_NAME' => '/index.php', 'REQUEST_URI' => '/foo', 'REQUEST_METHOD' => 'GET', 'REMOTE_ADDR' => '127.0.0.2']);
$uri = Uri::createFromEnvironment($env);
$headers = Headers::createFromEnvironment($env);
$headers->set('Accept', 'text/html');
$cookies = [];
$serverParams = $env->all();
$body = new Body(fopen('php://temp', 'r+'));
$req = new Request('GET', $uri, $headers, $cookies, $serverParams, $body);
$res = new Response();
$app->getContainer()['request'] = $req;
$app->getContainer()['response'] = $res;
// Set the options value.
$this->options = ['error_code' => 403, 'exception_message' => 'NOT ALLOWED'];
$app->add(new IpRestrictMiddleware($this->ipSet, false, $this->options));
$appMessage = 'I am In';
$app->get('/foo', function ($req, $res) use($appMessage) {
$res->write($appMessage);
return $res;
});
$resOut = $app->run();
$body = (string) $resOut->getBody();
$this->assertEquals($appMessage, $body, 'The client is allowed to access the application.');
}
示例13: renderAjax
public function renderAjax($view, $params = [])
{
$render = $this->getView()->render($view, $params);
return $this->response->write($render);
}