本文整理汇总了PHP中Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent::getRequestType方法的典型用法代码示例。如果您正苦于以下问题:PHP GetResponseForControllerResultEvent::getRequestType方法的具体用法?PHP GetResponseForControllerResultEvent::getRequestType怎么用?PHP GetResponseForControllerResultEvent::getRequestType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent
的用法示例。
在下文中一共展示了GetResponseForControllerResultEvent::getRequestType方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: onView
/**
* Processes a successful controller into an HTTP 200 response.
*
* Some controllers may not return a response object but simply the body of
* one. The VIEW event is called in that case, to allow us to mutate that
* body into a Response object. In particular we assume that the return
* from an JSON-type response is a JSON string, so just wrap it into a
* Response object.
*
* @param Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent $event
* The Event to process.
*/
public function onView(GetResponseForControllerResultEvent $event)
{
$request = $event->getRequest();
// For a master request, we process the result and wrap it as needed.
// For a subrequest, all we want is the string value. We assume that
// is just an HTML string from a controller, so wrap that into a response
// object. The subrequest's response will get dissected and placed into
// the larger page as needed.
if ($event->getRequestType() == HttpKernelInterface::MASTER_REQUEST) {
$method = 'on' . $this->negotiation->getContentType($request);
if (method_exists($this, $method)) {
$event->setResponse($this->{$method}($event));
} else {
$event->setResponse(new Response('Not Acceptable', 406));
}
} else {
// This is a new-style Symfony-esque subrequest, which means we assume
// the body is not supposed to be a complete page but just a page
// fragment.
$page_result = $event->getControllerResult();
if ($page_result instanceof HtmlPage || $page_result instanceof Response) {
return $page_result;
}
if (!is_array($page_result)) {
$page_result = array('#markup' => $page_result);
}
// If no title was returned fall back to one defined in the route.
if (!isset($page_result['#title'])) {
$page_result['#title'] = $this->titleResolver->getTitle($request, $request->attributes->get(RouteObjectInterface::ROUTE_OBJECT));
}
$event->setResponse(new Response(drupal_render_root($page_result)));
}
}
示例2: onKernelResultView
/**
* @param \Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent $event
*
* @throws \Exception
*/
public function onKernelResultView(GetResponseForControllerResultEvent $event)
{
if ($event->getRequestType() !== HttpKernelInterface::MASTER_REQUEST) {
return;
}
if (!$this->isRestRequest($event->getRequest())) {
return;
}
$result = $event->getControllerResult();
$event->setResponse($this->visitResult($result));
$event->stopPropagation();
}
示例3: onKernelView
public function onKernelView(GetResponseForControllerResultEvent $event)
{
if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
return;
}
$result = $event->getControllerResult();
if (!is_array($result)) {
return;
}
$result = $this->factory->create($this->resolver->resolve($event->getRequest(), $result), $result);
$event->setResponse(new Response($result));
}
示例4: onView
/**
* Processes a successful controller into an HTTP 200 response.
*
* Some controllers may not return a response object but simply the body of
* one. The VIEW event is called in that case, to allow us to mutate that
* body into a Response object. In particular we assume that the return
* from an JSON-type response is a JSON string, so just wrap it into a
* Response object.
*
* @param \Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent $event
* The Event to process.
*/
public function onView(GetResponseForControllerResultEvent $event)
{
$request = $event->getRequest();
if ($event->getRequestType() == HttpKernelInterface::MASTER_REQUEST) {
$method = 'on' . $request->getRequestFormat();
if (method_exists($this, $method)) {
$event->setResponse($this->{$method}($event));
} else {
$event->setResponse(new Response('Not Acceptable', 406));
}
}
}
示例5: onKernelView
/**
* This event is called when the controller action returns something else than a response object (e.g. an array).
* This function handles the rendering of proper template according to the requested partial.
*
* @param GetResponseForControllerResultEvent $event Event called.
*/
public function onKernelView(GetResponseForControllerResultEvent $event)
{
// only work with master request
if ($event->getRequestType() !== HttpKernelInterface::MASTER_REQUEST) {
return;
}
// only on NJAX requests
$njax = $this->container->get('njax');
if (!$njax->isNjax()) {
return;
}
$request = $event->getRequest();
$parameters = $event->getControllerResult();
if (!($njaxConfiguration = $request->attributes->get('_njax'))) {
return;
}
// this behavior is copied from Sensio\Bundle\FrameworkExtraBundle\EventListener\TemplateListener::onKernelView()
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;
}
// get the templating engine
$templating = $this->container->get('templating');
// now figure out which template to use
$partial = $njax->getPartial();
$template = $njaxConfiguration->getPartialTemplate($partial);
$usedPartial = $partial;
// if no specified template then try to guess the name of a partial template
if (!$template) {
$template = $njaxConfiguration->guessPartialTemplate($partial);
// if couldn't guess the name then just use the default one
if (!$template || !$templating->exists($template)) {
$template = $njaxConfiguration->getDefaultTemplate();
$usedPartial = null;
}
}
$content = $templating->render($template, $parameters);
$event->setResponse(new Response($content));
}