本文整理汇总了PHP中Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent::getRequest方法的典型用法代码示例。如果您正苦于以下问题:PHP GetResponseForControllerResultEvent::getRequest方法的具体用法?PHP GetResponseForControllerResultEvent::getRequest怎么用?PHP GetResponseForControllerResultEvent::getRequest使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent
的用法示例。
在下文中一共展示了GetResponseForControllerResultEvent::getRequest方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: 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);
}
示例2: onKernelResultView
/**
* @param \Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent $event
*/
public function onKernelResultView(GetResponseForControllerResultEvent $event)
{
if (!$event->getRequest()->attributes->get('is_rest_request')) {
return;
}
$event->setResponse($this->viewDispatcher->dispatch($event->getRequest(), $event->getControllerResult()));
$event->stopPropagation();
}
示例3: onKernelView
public function onKernelView(GetResponseForControllerResultEvent $event)
{
$this->logger->debug('Response builder listener: catch kernel.request event');
// If this is not a master request, skip handling
if (!$event->isMasterRequest()) {
$this->logger->debug('Response builder listener: this is not master request, skip');
return;
}
// If content already prepared
if ($event->hasResponse()) {
$this->logger->debug('Response builder listener: event already has response, skip');
return;
}
// Getting controller result
$result = $event->getControllerResult();
// If result is Response instance
if ($result instanceof Response) {
$this->logger->debug('Response builder listener: controller result is already an instance of Response, skip');
return;
}
// Getting action
$apiServerAction = $event->getRequest()->attributes->get('apiAction');
/* @var $apiServerAction ApiServerAction */
// Something wrong
if (!$apiServerAction) {
$this->logger->error('Response parser listener: request has no apiAction attribute, throwing access denied exception');
throw new AccessDeniedHttpException();
}
// Getting api server interface
$apiServerInterface = $apiServerAction->getApiServerInterface();
// Getting connection
$apiServerConnection = $event->getRequest()->attributes->get('apiConnection');
/* @var $apiServerConnection ApiServerConnection */
// Something wrong
if (!$apiServerConnection) {
$this->logger->error('Response parser listener: request has no apiConnection attribute, throwing access denied exception');
throw new AccessDeniedHttpException();
}
// Creating api response
try {
$apiResponse = $apiServerInterface->getApiResponse($result);
$newApiUserToken = $apiServerConnection->getNewApiUserToken();
if ($newApiUserToken) {
$this->logger->debug('Response builder listener: applying new api user token to response');
$apiResponse->setUserToken($newApiUserToken);
}
} catch (\Exception $e) {
$this->logger->error(sprintf('Response parser listener: problem with building response ("%s"), sending exception response', $e->getMessage()));
$apiResponse = $apiServerInterface->getExceptionResponse($e->getMessage());
}
// Setting response
$event->setResponse(new JsonResponse($apiResponse->export()));
$this->logger->debug('Response builder listener: response built');
}
示例4: onKernelView
/**
* Validates data returned by the controller if applicable.
*
* @param GetResponseForControllerResultEvent $event
*
* @return mixed
*
* @throws ValidationException
*/
public function onKernelView(GetResponseForControllerResultEvent $event)
{
$resourceType = $event->getRequest()->attributes->get('_resource_type');
if (!$resourceType || !in_array($event->getRequest()->getMethod(), [Request::METHOD_POST, Request::METHOD_PUT])) {
return;
}
$violations = $this->validator->validate($event->getControllerResult(), null, $resourceType->getValidationGroups());
if (0 !== count($violations)) {
throw new ValidationException($violations);
}
}
示例5: onKernelView
/**
* Renders the template and initializes a new response object with the
* rendered template content.
*
* @param GetResponseForControllerResultEvent $event
*/
public function onKernelView(GetResponseForControllerResultEvent $event)
{
/* @var Template $template */
$request = $event->getRequest();
$template = $request->attributes->get('_template');
if (null === $template) {
return;
}
$parameters = $event->getControllerResult();
$owner = $template->getOwner();
list($controller, $action) = $owner;
// when the annotation declares no default vars and the action returns
// null, all action method arguments are used as default vars
if (null === $parameters) {
$parameters = $this->resolveDefaultParameters($request, $template, $controller, $action);
}
// attempt to render the actual response
$templating = $this->container->get('templating');
if ($template->isStreamable()) {
$callback = function () use($templating, $template, $parameters) {
return $templating->stream($template->getTemplate(), $parameters);
};
$event->setResponse(new StreamedResponse($callback));
}
// make sure the owner (controller+dependencies) is not cached or stored elsewhere
$template->setOwner(array());
$event->setResponse($templating->renderResponse($template->getTemplate(), $parameters));
}
示例6: 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);
}
示例7: 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));
}
}
示例8: 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);
}
示例9: 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);
}
示例10: onKernelView
/**
* Validates data returned by the controller if applicable.
*
* @param GetResponseForControllerResultEvent $event
*
* @throws ValidationException
*/
public function onKernelView(GetResponseForControllerResultEvent $event)
{
$request = $event->getRequest();
try {
$attributes = RequestAttributesExtractor::extractAttributes($request);
} catch (RuntimeException $e) {
return;
}
if ($request->isMethodSafe() || $request->isMethod(Request::METHOD_DELETE)) {
return;
}
$data = $event->getControllerResult();
$resourceMetadata = $this->resourceMetadataFactory->create($attributes['resource_class']);
if (isset($attributes['collection_operation_name'])) {
$validationGroups = $resourceMetadata->getCollectionOperationAttribute($attributes['collection_operation_name'], 'validation_groups');
} else {
$validationGroups = $resourceMetadata->getItemOperationAttribute($attributes['item_operation_name'], 'validation_groups');
}
if (!$validationGroups) {
// Fallback to the resource
$validationGroups = $resourceMetadata->getAttributes()['validation_groups'] ?? null;
}
if (is_callable($validationGroups)) {
$validationGroups = call_user_func_array($validationGroups, [$data]);
}
$violations = $this->validator->validate($data, null, $validationGroups);
if (0 !== count($violations)) {
throw new ValidationException($violations);
}
}
示例11: onKernelView
/**
* Renders the template and initializes a new response object with the
* rendered template content.
*
* @param GetResponseForControllerResultEvent $event A GetResponseForControllerResultEvent instance
*/
public function onKernelView(GetResponseForControllerResultEvent $event)
{
$request = $event->getRequest();
$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);
}
}
if (!is_array($parameters)) {
return $parameters;
}
if (!($template = $request->attributes->get('_template'))) {
return $parameters;
}
$templating = $this->container->get('templating');
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));
}
}
示例12: onKernelView
public function onKernelView(GetResponseForControllerResultEvent $e)
{
if (!$this->isRestRoute($e->getRequest())) {
return;
}
$e->getDispatcher()->dispatch('sdispatcher.rest_view', $e);
}
示例13: onKernelView
/**
* @param GetResponseForControllerResultEvent $event
*/
public function onKernelView(GetResponseForControllerResultEvent $event)
{
$request = $event->getRequest();
$nodeTranslation = $request->attributes->get('_nodeTranslation');
if ($nodeTranslation) {
$entity = $request->attributes->get('_entity');
$url = $request->attributes->get('url');
$nodeMenu = $request->attributes->get('_nodeMenu');
$parameters = $request->attributes->get('_renderContext');
if ($request->get('preview') == true) {
$version = $request->get('version');
if (!empty($version) && is_numeric($version)) {
$nodeVersion = $this->em->getRepository('KunstmaanNodeBundle:NodeVersion')->find($version);
if (!is_null($nodeVersion)) {
$entity = $nodeVersion->getRef($this->em);
}
}
}
$renderContext = array('nodetranslation' => $nodeTranslation, 'slug' => $url, 'page' => $entity, 'resource' => $entity, 'nodemenu' => $nodeMenu);
if (is_array($parameters) || $parameters instanceof \ArrayObject) {
$parameters = array_merge($renderContext, (array) $parameters);
} else {
$parameters = $renderContext;
}
// Sent the response here, another option is to let the symfony kernel.view listener handle it
$event->setResponse($this->templating->renderResponse($entity->getDefaultView(), $parameters));
}
}
示例14: 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));
}
}
示例15: onKernelView
/**
* In an API context, converts any data to a JSON-LD response.
*
* @param GetResponseForControllerResultEvent $event
*
* @return JsonLdResponse|mixed
*/
public function onKernelView(GetResponseForControllerResultEvent $event)
{
$controllerResult = $event->getControllerResult();
if ($controllerResult instanceof Response) {
return;
}
$request = $event->getRequest();
$format = $request->attributes->get('_api_format');
if (self::FORMAT !== $format) {
return;
}
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 JsonLdResponse($resourceType ? $this->normalizer->normalize($controllerResult, self::FORMAT, $resourceType->getNormalizationContext() + ['request_uri' => $request->getRequestUri()]) : $controllerResult, $status);
$event->setResponse($response);
}