How to clear cache in Controller in Symfony3.4?
Asked Answered
A

2

5

after migration Symfony from 3.3 to 3.4, my function not working (it works before). I have to clear cache in controller, and when I execute command below, function returns error.

exec(sprintf(
  "php %s/bin/console cache:clear --env=prod",
  $this->getParameter('kernel.project_dir')
));

It returns something like that:

Fatal error: require(): Failed opening required '/[...]/var/cache/prod/ContainerAcrshql/getTwig_ExceptionListenerService.php' (include_path='.:/usr/local/share/pear') in /[...]/var/cache/prod/ContainerAcrshql/appProdProjectContainer.php on line 764 Fatal error: require(): Failed opening required '/[...]/var/cache/prod/ContainerAcrshql/getSwiftmailer_EmailSender_ListenerService.php' (include_path='.:/usr/local/share/pear') in /[...]/var/cache/prod/ContainerAcrshql/appProdProjectContainer.php on line 764

In addition I can tell You, that in dev environment it works properly. Also when project run localy and simulate prod env (in address bar I type app.php after localhost:8000). I haven't other server to check if problem still occured

Abie answered 9/1, 2018 at 10:33 Comment(12)
Are you sure that this is the cache that you want to clear? What are you doing that requires clearing this cache?Sheffy
Im sure, of course. I allow to change translations files by admin panel. I store global settings in yml files, and also user can change it by admin panelAbie
Use database for that like every sane person would. You don't want to cause downtime just because of changed translation.Sheffy
In previous Symfony version it works. Application is online, it is no time to rebuild all application :(Abie
If by "previous version" you mean Symfony2, then sure, you're most likely doing wrong right now... Symfony3 have a built-in translation... Enable it... symfony.com/doc/3.4/translation.html. As for your cache problem, it's a permission issue. Check how to set proper permission for a web server. ;)Easement
@Easement previous version means Symfony3.3. Application ran about a month and than I made upgrade to 3.4. After that it was broken. Im using Symfony translations + jms/i18n-routing-bundle, but user defines the meaning of single words. So even if whole project has 777 permissions, the problem still occuredAbie
Symfony 3.3 and 3.4 aren't compatible (don't know why)... If you used composer to upgrade, then return to 3.3. I had the same issue with one of my projet, going from 3.3 to 3.4 broke everything. If you really want to use 3.4, then create a new symfony project, and move all your entities, view, forms, etc. that should do the trick... ;)Easement
@Easement - While I don't know the answer to this particular issue to say that 3.4 is not compatible with 3.3 is just plain silly. Lots of things can go wrong when you bump up a version.Pumice
@preciel i've done this with composer link and it seems that everything its ok (except this cache...)Abie
@Abie Yeah, I did as well, and like you I had an error and couldn't clear cache. I'm not suited to figure out the problem, thus I suggest that you turn back to 3.3 (hopping you did back up the composer.json file). Else you might want to download a 3.4 and copy/paste all the files in the vendor folder. If it's not a permissions problem, then it can only come from Symfony itself I think.Easement
@Abie just in case, did you try to manually delete the content of /var/cache in your Symfony project? If it solve you problem, then the issue really is permissions related.Easement
@Easement - I know this is off topic but I have moved a number of apps from 3.x to 3.4 by editing composer.json. And I just did a fresh install of 3.3 and moved it to 3.4 with no problem. I understand there can be difficulties and I often end up in composer hell myself and I would never try to jump a major version but the minor version stuff pretty much works out of the box at least.Pumice
T
9

I'm calling an already implemented Symfony's command that clear or warmup cache (tested on Symfony 4).

use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\KernelInterface;

class CommandController extends AbstractController
{

    /**
     *
     * @Route("/command/cache/clear", name="command_cache_clear")
     */
    public function command_cache_clear(KernelInterface $kernel)
    {
        return $this->do_command($kernel, 'cache:clear');
    }

    /**
     *
     * @Route("/command/cache/warmup", name="command_cache_warmup")
     */
    public function command_cache_warmup(KernelInterface $kernel)
    {
        return $this->do_command($kernel, 'cache:warmup');
    }

    private function do_command($kernel, $command)
    {
        $env = $kernel->getEnvironment();
        
        $application = new Application($kernel);
        $application->setAutoExit(false);
        
        $input = new ArrayInput(array(
            'command' => $command,
            '--env' => $env
        ));
        
        $output = new BufferedOutput();
        $application->run($input, $output);
        
        $content = $output->fetch();
        
        return new Response($content);
    }
}
Translocate answered 23/11, 2018 at 12:46 Comment(0)
M
0

You should add a valid permissions to the var/ directory to access to cache files:

chmod ... var/ -R

The user used when accessing from web is www-data

Moschatel answered 9/1, 2018 at 10:53 Comment(4)
that was the first thing I did when I detected the errorAbie
So you should verify that kernel.project_dir contain a valid path to the projectMoschatel
You can replace $this->getParameter('kernel.project_dir') by __DIR__ . '/../../..'Moschatel
it was next try. I also check command rm -rf ~/[...]/var/cache and I tried deleting via 'Symfony\Component\Filesystem\Filesystem' method removeAbie

© 2022 - 2024 — McMap. All rights reserved.