在Akeneo v6.x的事件订阅中,您可能会遇到“This url is not allowed”(此URL不允许)错误。要解决此问题,您可以尝试使用以下代码添加允许的URL:
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class MySubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
'my_event' => 'onMyEvent',
];
}
public function onMyEvent(Event $event)
{
$allowedUrl = 'https://mywebsite.com'; // 添加你的URL
if ($event->isPropagationStopped()) {
return;
}
$url = $event->getRequest()->server->get('HTTP_REFERER');
if ($url !== $allowedUrl) {
throw new \RuntimeException(sprintf('Invalid url "%s".', $url));
}
// ... 程序代码继续执行
}
}
在上例中,$allowedUrl变量是一个可以访问该事件的URL。您可以根据自己的需要进行修改。如果请求来自此URL,则程序代码将继续执行。否则,将引发RuntimeException。