本文整理汇总了PHP中FOS\RestBundle\View\View::getStatusCode方法的典型用法代码示例。如果您正苦于以下问题:PHP View::getStatusCode方法的具体用法?PHP View::getStatusCode怎么用?PHP View::getStatusCode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类FOS\RestBundle\View\View
的用法示例。
在下文中一共展示了View::getStatusCode方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getStatusCode
/**
* FOSRest's ViewHandler has a private getStatusCode method. Because we cannot use it, we added
* this lightweight method (without forms support) for Hal custom handler.
*
* @param View $view view instance
*
* @return int HTTP status code
*/
private function getStatusCode(View $view)
{
if (null !== ($code = $view->getStatusCode())) {
return $code;
}
return Codes::HTTP_OK;
}
示例2: createRedirectResponse
public function createRedirectResponse(View $view, $location, $format)
{
if ($format == 'html' && $view->getData() != null && ($view->getStatusCode() == Codes::HTTP_CREATED || $view->getStatusCode() == Codes::HTTP_ACCEPTED)) {
$prevStatus = $view->getStatusCode();
$view->setStatusCode(Codes::HTTP_OK);
}
$response = parent::createRedirectResponse($view, $location, $format);
if ($response->headers->has('Location') && $response->getStatusCode() !== Codes::HTTP_CREATED && ($response->getStatusCode() < 300 || $response->getStatusCode() >= 400)) {
$response->headers->remove('Location');
}
if (isset($prevStatus)) {
$view->setStatusCode($prevStatus);
$code = isset($this->forceRedirects[$format]) ? $this->forceRedirects[$format] : $this->getStatusCode($view, $response->getContent());
$response->setStatusCode($code);
}
return $response;
}
示例3: onKernelView
/**
* Renders the parameters and template and initializes a new response object with the
* rendered content.
*
* @param GetResponseForControllerResultEvent $event A GetResponseForControllerResultEvent instance
*/
public function onKernelView(GetResponseForControllerResultEvent $event)
{
$request = $event->getRequest();
$configuration = $request->attributes->get('_view');
$view = $event->getControllerResult();
if (!$view instanceof View) {
if (!$configuration && !$this->container->getParameter('fos_rest.view_response_listener.force_view')) {
return parent::onKernelView($event);
}
$view = new View($view);
}
if ($configuration) {
if ($configuration->getTemplateVar()) {
$view->setTemplateVar($configuration->getTemplateVar());
}
if (null === $view->getStatusCode() && $configuration->getStatusCode()) {
$view->setStatusCode($configuration->getStatusCode());
}
if ($configuration->getSerializerGroups()) {
$context = $view->getSerializationContext() ?: new SerializationContext();
$context->setGroups($configuration->getSerializerGroups());
$view->setSerializationContext($context);
}
}
if (null === $view->getFormat()) {
$view->setFormat($request->getRequestFormat());
}
$vars = $request->attributes->get('_template_vars');
if (!$vars) {
$vars = $request->attributes->get('_template_default_vars');
}
$viewHandler = $this->container->get('fos_rest.view_handler');
if ($viewHandler->isFormatTemplating($view->getFormat())) {
if (!empty($vars)) {
$parameters = (array) $viewHandler->prepareTemplateParameters($view);
foreach ($vars as $var) {
if (!array_key_exists($var, $parameters)) {
$parameters[$var] = $request->attributes->get($var);
}
}
$view->setData($parameters);
}
$template = $request->attributes->get('_template');
if ($template) {
if ($template instanceof TemplateReference) {
$template->set('format', null);
}
$view->setTemplate($template);
}
}
$response = $viewHandler->handle($view, $request);
$event->setResponse($response);
}
示例4: createRedirectResponse
/**
* Creates the Response from the view.
*
* @param View $view
* @param string $location
* @param string $format
*
* @return Response
*/
public function createRedirectResponse(View $view, $location, $format)
{
$content = null;
if (($view->getStatusCode() == Response::HTTP_CREATED || $view->getStatusCode() == Response::HTTP_ACCEPTED) && $view->getData() != null) {
$response = $this->initResponse($view, $format);
} else {
$response = $view->getResponse();
if ('html' === $format && isset($this->forceRedirects[$format])) {
$redirect = new RedirectResponse($location);
$content = $redirect->getContent();
$response->setContent($content);
}
}
$code = isset($this->forceRedirects[$format]) ? $this->forceRedirects[$format] : $this->getStatusCode($view, $content);
$response->setStatusCode($code);
$response->headers->set('Location', $location);
return $response;
}
示例5: isEntityEmpty
/**
* @return bool
*/
public function isEntityEmpty()
{
return null === $this->view->getData() || $this->view->getStatusCode() != self::STATUS_OK && $this->view->getStatusCode() != self::STATUS_OK;
}
示例6: onKernelView
/**
* Renders the parameters and template and initializes a new response object with the
* rendered content.
*
* @param GetResponseForControllerResultEvent $event
*/
public function onKernelView(GetResponseForControllerResultEvent $event)
{
$request = $event->getRequest();
/** @var \FOS\RestBundle\Controller\Annotations\View $configuration */
$configuration = $request->attributes->get('_view');
$view = $event->getControllerResult();
$customViewDefined = true;
if (!$view instanceof View) {
if (!$configuration && !$this->container->getParameter('fos_rest.view_response_listener.force_view')) {
return parent::onKernelView($event);
}
$view = new View($view);
$customViewDefined = false;
}
if ($configuration) {
if ($configuration->getTemplateVar()) {
$view->setTemplateVar($configuration->getTemplateVar());
}
if ($configuration->getStatusCode() && (null === $view->getStatusCode() || Codes::HTTP_OK === $view->getStatusCode())) {
$view->setStatusCode($configuration->getStatusCode());
}
if ($configuration->getSerializerGroups() && !$customViewDefined) {
$context = $view->getSerializationContext() ?: new SerializationContext();
$context->setGroups($configuration->getSerializerGroups());
$view->setSerializationContext($context);
}
if ($configuration->getSerializerEnableMaxDepthChecks()) {
$context = $view->getSerializationContext() ?: new SerializationContext();
$context->enableMaxDepthChecks();
$view->setSerializationContext($context);
}
$populateDefaultVars = $configuration->isPopulateDefaultVars();
} else {
$populateDefaultVars = true;
}
if (null === $view->getFormat()) {
$view->setFormat($request->getRequestFormat());
}
$vars = $request->attributes->get('_template_vars');
if (!$vars && $populateDefaultVars) {
$vars = $request->attributes->get('_template_default_vars');
}
$viewHandler = $this->container->get('fos_rest.view_handler');
if ($viewHandler->isFormatTemplating($view->getFormat())) {
if (!empty($vars)) {
$parameters = (array) $viewHandler->prepareTemplateParameters($view);
foreach ($vars as $var) {
if (!array_key_exists($var, $parameters)) {
$parameters[$var] = $request->attributes->get($var);
}
}
$view->setData($parameters);
}
$template = $request->attributes->get('_template');
if ($template) {
if ($template instanceof TemplateReference) {
$template->set('format', null);
}
$view->setTemplate($template);
}
}
$response = $viewHandler->handle($view, $request);
$event->setResponse($response);
}
示例7: testSetStatusCode
public function testSetStatusCode()
{
$view = new View();
$code = 404;
$view->setStatusCode($code);
$this->assertEquals($code, $view->getStatusCode());
}
示例8: getStatusCode
/**
* Gets a response HTTP status code from a View instance
*
* By default it will return 200. However if there is a FormInterface stored for
* the key 'form' in the View's data it will return the failed_validation
* configuration if the form instance has errors.
*
* @param View $view view instance
* @param mixed $content
*
* @return int HTTP status code
*/
protected function getStatusCode(View $view, $content = null)
{
if (200 !== ($code = $view->getStatusCode())) {
return $code;
}
$data = $view->getData();
if ($data instanceof FormInterface) {
$form = $data;
} elseif (is_array($data) && isset($data['form']) && $data['form'] instanceof FormInterface) {
$form = $data['form'];
} else {
$form = false;
}
if ($form && $form->isBound() && !$form->isValid()) {
return $this->failedValidationCode;
}
return null !== $content ? Codes::HTTP_OK : $this->emptyContentCode;
}
示例9: getStatusCode
/**
* Gets a response HTTP status code from a View instance
*
* By default it will return 200. However if there is a FormInterface stored for
* the key 'form' in the View's data it will return the failed_validation
* configuration if the form instance has errors.
*
* @param View $view view instance
*
* @return int HTTP status code
*/
private function getStatusCode(View $view)
{
if (null !== ($code = $view->getStatusCode())) {
return $code;
}
$data = $view->getData();
if ($data instanceof FormInterface) {
$form = $data;
} elseif (is_array($data) && isset($data['form']) && $data['form'] instanceof FormInterface) {
$form = $data['form'];
} else {
$form = false;
}
return $form && $form->isBound() && !$form->isValid() ? $this->failedValidationCode : Codes::HTTP_OK;
}
示例10: onKernelView
/**
* Renders the parameters and template and initializes a new response object with the
* rendered content.
*
* @param GetResponseForControllerResultEvent $event
*/
public function onKernelView(GetResponseForControllerResultEvent $event)
{
$request = $event->getRequest();
if (!$request->attributes->get(FOSRestBundle::ZONE_ATTRIBUTE, true)) {
return false;
}
/** @var \FOS\RestBundle\Controller\Annotations\View $configuration */
$configuration = $request->attributes->get('_view');
$view = $event->getControllerResult();
$customViewDefined = true;
if (!$view instanceof View) {
if (!$configuration && !$this->forceView) {
return;
}
$view = new View($view);
$customViewDefined = false;
}
if ($configuration) {
if ($configuration->getTemplateVar()) {
$view->setTemplateVar($configuration->getTemplateVar());
}
if ($configuration->getStatusCode() && (null === $view->getStatusCode() || Response::HTTP_OK === $view->getStatusCode())) {
$view->setStatusCode($configuration->getStatusCode());
}
$context = $view->getContext();
if ($configuration->getSerializerGroups() && !$customViewDefined) {
$context->addGroups($configuration->getSerializerGroups());
}
if ($configuration->getSerializerEnableMaxDepthChecks()) {
$context->setMaxDepth(0);
}
$populateDefaultVars = $configuration->isPopulateDefaultVars();
} else {
$populateDefaultVars = true;
}
if (null === $view->getFormat()) {
$view->setFormat($request->getRequestFormat());
}
$vars = $request->attributes->get('_template_vars');
if (!$vars && $populateDefaultVars) {
$vars = $request->attributes->get('_template_default_vars');
}
if ($this->viewHandler->isFormatTemplating($view->getFormat()) && !$view->getRoute() && !$view->getLocation()) {
if (!empty($vars)) {
$parameters = (array) $this->viewHandler->prepareTemplateParameters($view);
foreach ($vars as $var) {
if (!array_key_exists($var, $parameters)) {
$parameters[$var] = $request->attributes->get($var);
}
}
$view->setData($parameters);
}
$template = null !== $configuration && $configuration->getTemplate() ? $configuration->getTemplate() : $request->attributes->get('_template');
if ($template && !$view->getTemplate()) {
if ($template instanceof TemplateReferenceInterface) {
$template->set('format', null);
}
$view->setTemplate($template);
}
}
$response = $this->viewHandler->handle($view, $request);
$event->setResponse($response);
}
示例11: getStatusCode
/**
* Gets a response HTTP status code from a View instance
*
* By default it will return 200. However if there is a FormInterface stored for
* the key 'form' in the View's data it will return the failed_validation
* configuration if the form instance has errors.
*
* @param View $view view instance
* @return int HTTP status code
*/
private function getStatusCode(View $view)
{
if (null !== ($code = $view->getStatusCode())) {
return $code;
}
$data = $view->getData();
if (!is_array($data) || empty($data['form']) || !$data['form'] instanceof FormInterface) {
return Codes::HTTP_OK;
}
return $data['form']->isBound() && !$data['form']->isValid() ? $this->failedValidationCode : Codes::HTTP_OK;
}
示例12: statusCodeMatches
private function statusCodeMatches(View $subject, $argument)
{
if (isset($argument['statusCode'])) {
return $subject->getStatusCode() === $argument['statusCode'];
}
return true;
}
示例13: testGetStatusCodeFromResponse
public function testGetStatusCodeFromResponse()
{
$view = new View();
$this->assertNull($view->getStatusCode());
$this->assertEquals(Response::HTTP_OK, $view->getResponse()->getStatusCode());
// default code of the response.
}