PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, October 18, 2022

[FIXED] How can I use the a cache manager in Symfony?

 October 18, 2022     caching, methods, sulu, symfony     No comments   

Issue

I my contoller I try to clear the cache when updating a page

protected function mapDataToEntity(array $data, Project $entity): void{
    
    $entity->setName($data['name']);

    $cacheManager = $this->get('sulu_http_cache.cache_manager');
    $cacheManager->invalidatePath($path, $headers);
}

I get the error message:

Attempted to call an undefined method named "get" of class "App\Controller\Admin\ProjectController". Did you mean to call e.g. "cgetAction", "getAction", "getLocale" or "getSecurityContext"?


Solution

You should autowire your cache manager instead of trying to access it from the container.

private CacheManager $cacheManager;

public function __construct(CacheManager $cacheManager)
{
    $this->cacheManager = $cacheManager;
}

And use it in your method:

$this->cacheManager->invalidatePath($path, $headers);


Answered By - Dylan KAS
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing