本文整理汇总了PHP中Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent::setResponse方法的典型用法代码示例。如果您正苦于以下问题:PHP GetResponseForControllerResultEvent::setResponse方法的具体用法?PHP GetResponseForControllerResultEvent::setResponse怎么用?PHP GetResponseForControllerResultEvent::setResponse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent
的用法示例。
在下文中一共展示了GetResponseForControllerResultEvent::setResponse方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: onKernelView
/**
* @param GetResponseForControllerResultEvent $event The event to handle
*/
public function onKernelView(GetResponseForControllerResultEvent $event)
{
/**
* Anytime we don't find what we're looking for, return and let other
* listeners handle things
*/
$response = $event->getControllerResult();
if (!is_array($response)) {
return;
}
$request = $event->getRequest();
$routeName = $request->attributes->get('_route');
if (!($route = $this->app['routes']->get($routeName))) {
return;
}
if (!($args = $route->getOption('_view'))) {
return;
}
$template = $args['template'];
$callable = $args['callable'];
if ($callable) {
$response = $callable($response, $request);
}
if ($response instanceof Response) {
$event->setResponse($response);
return;
}
if (null !== $template) {
$output = $this->render($template, $response);
$event->setResponse(new Response($output));
}
}
示例2: onKernelView
public function onKernelView(GetResponseForControllerResultEvent $event)
{
if (!$event->isMasterRequest()) {
return;
}
$request = $event->getRequest();
if ('json' !== $request->getRequestFormat()) {
return;
}
$result = $event->getControllerResult();
if (null === $result) {
$event->setResponse(new JsonResponse(null, 204));
return;
}
if ($result instanceof Response) {
return;
}
$response = new JsonResponse();
if ($request->isMethod('POST')) {
$response->setStatusCode(201);
}
if (!is_scalar($result)) {
$result = $this->getSerializer()->normalize($result, null, ['groups' => $this->options['serialization_default_groups']]);
}
$response->setData($result);
$event->setResponse($response);
}
示例3: onKernelView
/**
* Handle converting the result into a valid JSON Response object back to the caller.
*
* @param GetResponseForControllerResultEvent $event The event to handle
*/
public function onKernelView(GetResponseForControllerResultEvent $event)
{
$response = $event->getControllerResult();
if ($response instanceof Response) {
$event->setResponse($response);
}
if (is_null($response)) {
$event->setResponse(new Response('', 404, $this->getResponseHeaders()));
return;
}
// we allow the response object that come back to be encoded by our default handler
// or decorator if need be.
if (is_object($response)) {
$decorator = $this->getDecorator();
if ($decorator) {
$decorator->setObject($response);
$event->setResponse(new Response($decorator->getResult(), 200, $this->getResponseHeaders()));
return;
}
if (is_callable($response, "toJson")) {
$event->setResponse(new Response($response->toJson(), 200, $this->getResponseHeaders()));
return;
}
}
$event->setResponse(new JsonResponse($response));
}
示例4: onKernelView
public function onKernelView(GetResponseForControllerResultEvent $event)
{
$request = $event->getRequest();
$templating = $this->templating;
$parameters = $event->getControllerResult();
if (null === $parameters) {
if (!($vars = $request->attributes->get('_template_vars'))) {
if (!($vars = $request->attributes->get('_template_default_vars'))) {
return;
}
}
$parameters = array();
foreach ($vars as $var) {
$parameters[$var] = $request->attributes->get($var);
}
}
$routeParams = $request->attributes->get('_route_params');
// get injected layout entity from annotation and add it to the paramaters
if (isset($routeParams['rootLayout'])) {
$parameters['rootLayout'] = $routeParams['rootLayout'];
}
if (!($template = $request->attributes->get('_template'))) {
return $parameters;
}
// wrap all parameters in params array
$parameters['params'] = $parameters;
if (!$request->attributes->get('_template_streamable')) {
$event->setResponse($templating->renderResponse($template, $parameters));
} else {
$callback = function () use($templating, $template, $parameters) {
return $templating->stream($template, $parameters);
};
$event->setResponse(new StreamedResponse($callback));
}
}
示例5: onViewTestDomainObject
/**
* Sets a response given a TestDomainObject instance.
*
* @param \Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent $event
* The event to process.
*/
public function onViewTestDomainObject(GetResponseForControllerResultEvent $event)
{
$result = $event->getControllerResult();
if ($result instanceof TestDomainObject) {
if ($result instanceof AttachmentsTestDomainObject) {
$event->setResponse(new AttachmentsTestResponse('AttachmentsTestDomainObject'));
} elseif ($result instanceof CacheableTestDomainObject) {
$event->setResponse(new CacheableTestResponse('CacheableTestDomainObject'));
} else {
$event->setResponse(new Response('TestDomainObject'));
}
}
}
示例6: onView
/**
* Transform data from controller to json.
*
* @param GetResponseForControllerResultEvent $event
*/
public function onView(GetResponseForControllerResultEvent $event)
{
$controllerResult = $event->getControllerResult();
if (is_string($controllerResult)) {
if (null === json_decode($controllerResult)) {
$event->setResponse(new Response($controllerResult));
return;
}
} else {
$controllerResult = $this->serializer->serialize($controllerResult, 'json');
}
$response = new JsonResponse();
$response->setJsonData($controllerResult);
$event->setResponse($response);
}
示例7: onView
/**
* Transform data from controller to json.
*
* @param GetResponseForControllerResultEvent $event
*/
public function onView(GetResponseForControllerResultEvent $event)
{
$controllerResult = $event->getControllerResult();
$response = new JsonResponse();
$response->setJsonData($this->serializer->serialize($controllerResult, 'json'));
$event->setResponse($response);
}
示例8: onKernelView
/**
* @param \Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent $e
*/
public function onKernelView(GetResponseForControllerResultEvent $e)
{
$queryset = $e->getControllerResult();
$routeName = $e->getRequest()->attributes->get('_route');
$route = $this->routes->get($routeName);
if (!$route) {
return;
}
$interface = 'SDispatcher\\Common\\PaginatorInterface';
$paginatorClass = $route->getOption(RouteOptions::PAGINATOR_CLASS);
if (!$paginatorClass || !is_subclass_of($paginatorClass, $interface)) {
return;
}
try {
/* @var \SDispatcher\Common\PaginatorInterface $paginator */
$paginator = new $paginatorClass();
if (!$paginator->supports($queryset)) {
return;
}
list($headers, $data) = $paginator->paginate($e->getRequest(), $queryset, 0, $route->getOption(RouteOptions::PAGE_LIMIT), $route->getOption(RouteOptions::PAGINATED_META_CONTAINER_NAME), $route->getOption(RouteOptions::PAGINATED_DATA_CONTAINER_NAME));
} catch (\Exception $ex) {
list($headers, $data) = array(array(), array());
}
$response = new DataResponse($data);
$response->headers->add($headers);
$e->setResponse($response);
}
示例9: onKernelView
/**
* In an API context, converts any data to a XML response.
*
* @param GetResponseForControllerResultEvent $event
*
* @return Response|mixed
*/
public function onKernelView(GetResponseForControllerResultEvent $event)
{
$controllerResult = $event->getControllerResult();
if ($controllerResult instanceof Response) {
return $controllerResult;
}
$request = $event->getRequest();
$format = $request->attributes->get('_api_format');
if (self::FORMAT !== $format) {
return $controllerResult;
}
switch ($request->getMethod()) {
case Request::METHOD_POST:
$status = 201;
break;
case Request::METHOD_DELETE:
$status = 204;
break;
default:
$status = 200;
break;
}
$resourceType = $request->attributes->get('_resource_type');
$response = new Response($this->serializer->serialize($controllerResult, self::FORMAT, $resourceType->getNormalizationContext()), $status, ['Content-Type' => 'application/xml']);
$event->setResponse($response);
}
示例10: onControllerView
/**
* @param GetResponseForControllerResultEvent $event
*/
public function onControllerView(GetResponseForControllerResultEvent $event)
{
if (!$event->getControllerResult() instanceof XmlRpcResponse) {
return;
}
$event->setResponse($this->responseGenerator->fromXmlRpcResponse($event->getControllerResult()));
}
示例11: onKernelView
/**
* Renders the layout and initializes the content of a new response object
* with the rendered layout.
*
* @param GetResponseForControllerResultEvent $event
*
* @throws LogicException if @Layout annotation is used in incorrect way
*/
public function onKernelView(GetResponseForControllerResultEvent $event)
{
$request = $event->getRequest();
/** @var LayoutAnnotation|null $layoutAnnotation */
$layoutAnnotation = $request->attributes->get('_layout');
if (!$layoutAnnotation) {
return;
}
if ($request->attributes->get('_template')) {
throw new LogicException('The @Template() annotation cannot be used together with the @Layout() annotation.');
}
$parameters = $event->getControllerResult();
if (is_array($parameters)) {
$context = new LayoutContext();
foreach ($parameters as $key => $value) {
$context->set($key, $value);
}
$this->configureContext($context, $layoutAnnotation);
$layout = $this->getLayout($context, $layoutAnnotation);
} elseif ($parameters instanceof ContextInterface) {
$this->configureContext($parameters, $layoutAnnotation);
$layout = $this->getLayout($parameters, $layoutAnnotation);
} elseif ($parameters instanceof Layout) {
if (!$layoutAnnotation->isEmpty()) {
throw new LogicException('The empty @Layout() annotation must be used when ' . 'the controller returns an instance of "Oro\\Component\\Layout\\Layout".');
}
$layout = $parameters;
} else {
return;
}
$response = new Response();
$response->setContent($layout->render());
$event->setResponse($response);
}
示例12: onKernelView
public function onKernelView(GetResponseForControllerResultEvent $event)
{
$response = $event->getResponse();
if ($response && $response->headers->get('Content-type') == 'application/json') {
return;
}
$controllerResult = $event->getControllerResult();
$response = new Response();
$response->setStatusCode(200);
$response->headers->add(array('Content-type' => 'application/json'));
list($controllerObject, $controllerMethod) = explode('::', $event->getRequest()->get('_controller'));
$method = new \ReflectionMethod($controllerObject, $controllerMethod);
$annotations = $this->reader->getMethodAnnotations($method);
$full = false;
foreach ($annotations as $annotation) {
if ($annotation instanceof \Hyper\AdsBundle\Api\Json) {
$full = $annotation->full;
}
}
if (is_object($controllerResult)) {
$content = json_encode($this->serializer->toJson($controllerResult, $full));
} elseif (is_array($controllerResult) && !empty($controllerResult) && is_object(current($controllerResult))) {
$content = json_encode($this->serializer->toJsonArray($controllerResult));
} else {
$content = json_encode($controllerResult);
}
$response->setContent($content);
$event->setResponse($response);
}
示例13: onKernelView
public function onKernelView(GetResponseForControllerResultEvent $event)
{
$response = $event->getControllerResult();
if (!$response instanceof Response) {
$event->setResponse(new Response($response));
}
}
示例14: onKernelView
public function onKernelView(GetResponseForControllerResultEvent $event)
{
$hal = $event->getControllerResult();
if ($hal instanceof Hal) {
$event->setResponse(new HalResponse($hal, 200, [], $this->prettyPrint));
}
}
示例15: onKernelView
public function onKernelView(GetResponseForControllerResultEvent $event)
{
$response = $event->getControllerResult();
if (is_object($response) && !$response instanceof Response || is_array($response)) {
$event->setResponse(new Response(serialize($response)));
}
}