プログラムでフロントページのキャッシュを無効にするにはどうすればよいですか?
それを達成する1つの方法は、'<front>'
routeSubscriberサービスを使用したルート。
mymodule.route_subscriber:
class: Drupal\mymodule\Routing\RouteSubscriber
arguments: ['@config.factory']
tags:
- { name: event_subscriber }
次に、RouteSubscriber.phpクラスで:
/**
* Event subscriber.
*/
namespace Drupal\mymodule\Routing;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\RouteCollection;
/**
* Subscribe to KernelEvents::REQUEST events and redirect if user is press.
*/
class RouteSubscriber extends RouteSubscriberBase {
protected $configFactory;
/**
* RouteSubscriber constructor.
* @param ConfigFactoryInterface $config_factory
*/
public function __construct(ConfigFactoryInterface $config_factory) {
$this->configFactory = $config_factory;
}
/**
* {@inheritdoc}
*/
public function alterRoutes(RouteCollection $collection) {
if ($route_front = $collection->get('<front>')) {
$route_front->setOption('no_cache', TRUE);
}
}
}