Skip to content

Instantly share code, notes, and snippets.

@hissy
Last active December 16, 2020 18:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hissy/5c6e93db17ad4753a4debee1b5c66866 to your computer and use it in GitHub Desktop.
Save hissy/5c6e93db17ad4753a4debee1b5c66866 to your computer and use it in GitHub Desktop.
[concrete5] [V8] Fix: IE tries to download json file when you hit save button of content block

How to fix: IE tries to download json file when you hit save button of content block

concrete5 uses Synfony Component to create http response. The Content-Type is application/json when it returns json response. Unfortunately, Internet Explorer can not understand application/json content type correctly. This fix to override Content Type header to 'text/plain' only for IE browser.

File list

  • application/bootstrap/autoload.php
  • application/config/app.php
  • application/src/Http/HttpServiceProvider.php
  • application/src/Http/ResponseFactory.php

References

<?php
return [
'providers' => [
'core_http' => \Application\Http\HttpServiceProvider::class
]
];
<?php
defined('C5_EXECUTE') or die('Access Denied.');
/*
* ----------------------------------------------------------------------------
* Load all composer autoload items.
* ----------------------------------------------------------------------------
*/
// If the checker class is already provided, likely we have been included in a separate composer project
if (!class_exists(\DoctrineXml\Checker::class)) {
// Otherwise, lets try to load composer ourselves
if (!@include(DIR_BASE_CORE . '/' . DIRNAME_VENDOR . '/autoload.php')) {
echo 'Third party libraries not installed. Make sure that composer has required libraries in the concrete/ directory.';
die(1);
}
}
// Modified from here
$classLoader = new \Symfony\Component\ClassLoader\Psr4ClassLoader();
$classLoader->addPrefix('Application\\Http', DIR_APPLICATION . '/' . DIRNAME_CLASSES . '/Http');
$classLoader->register();
<?php
namespace Application\Http;
use Concrete\Core\Http\ResponseFactoryInterface;
class HttpServiceProvider extends \Concrete\Core\Http\HttpServiceProvider
{
public function register()
{
parent::register();
// Override Response Factory
$this->app->bind(ResponseFactoryInterface::class, ResponseFactory::class);
}
}
<?php
namespace Application\Http;
use Concrete\Core\Http\Request;
use Concrete\Core\Http\Response;
class ResponseFactory extends \Concrete\Core\Http\ResponseFactory
{
/**
* @inheritDoc
*/
public function json($data, $code = Response::HTTP_OK, array $headers = [])
{
$response = parent::json($data, $code, $headers);
$request = Request::getInstance();
if (strpos($request->headers->get('User-Agent'), 'MSIE') !== false) {
$response->headers->set('Content-Type', 'text/plain');
}
return $response;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment